{-# OPTIONS_GHC -fno-warn-unused-do-bind #-} -- ihamletFile discards do results

module Jobs.Handler.SendNotification.Qualification
  ( dispatchNotificationQualificationExpiry
  , dispatchNotificationQualificationRenewal
  ) where

import Import


import Handler.Utils
import Jobs.Handler.SendNotification.Utils

-- import Handler.Info (FAQItem(..))
import qualified Data.CaseInsensitive as CI
import Text.Hamlet

-- import qualified Database.Esqueleto.Experimental as E
-- import qualified Database.Esqueleto.Utils as E


dispatchNotificationQualificationExpiry :: QualificationId -> Day -> UserId -> Handler ()
dispatchNotificationQualificationExpiry nQualification _nExpiry jRecipient = userMailT jRecipient $ do
  (User{..}, Qualification{..}, Entity _ QualificationUser{..}) <- liftHandler . runDB $ (,,)
    <$> getJust jRecipient
    <*> getJust nQualification
    <*> getJustBy (UniqueQualificationUser nQualification jRecipient)

  let qname = CI.original qualificationName
  $logDebugS "LMS" $ "Notify " <> tshow jRecipient <> " about expiry of qualification " <> qname
  replaceMailHeader "Auto-Submitted" $ Just "auto-generated"
  setSubjectI $ MsgMailSubjectQualificationExpiry qname
  
  editNotifications <- mkEditNotifications jRecipient

  addHtmlMarkdownAlternatives $(ihamletFile "templates/mail/qualificationExpiry.hamlet")


checkEmailOk :: a -> Bool
checkEmailOk = const True -- TODO 


dispatchNotificationQualificationRenewal :: QualificationId -> UserId -> Handler ()
dispatchNotificationQualificationRenewal nQualification jRecipient = do 
  (User{..}, Qualification{..}, Entity _ QualificationUser{..}) <- runDB $ (,,)
    <$> getJust jRecipient
    <*> getJust nQualification
    <*> getJustBy (UniqueQualificationUser nQualification jRecipient)
  let qname = CI.original qualificationName  
      -- content = $(i18nWidgetFile "qualification/renewal")
  $logDebugS "LMS" $ "Notify " <> tshow jRecipient <> " for renewal of qualifiaction " <> qname
  if | checkEmailOk userEmail -> userMailT jRecipient $ do
        
        replaceMailHeader "Auto-Submitted" $ Just "auto-generated"
        setSubjectI $ MsgMailSubjectQualificationRenewal qname
      
        editNotifications <- mkEditNotifications jRecipient -- TODO: add to hamlet file again         
        -- let msgrenewal = $(i18nHamletFile "qualification/renewal") -- :: HtmlUrlI18n (SomeMessage UniWorX) (Route UniWorX)
        -- addHtmlMarkdownAlternatives' msgrenewal
          
        -- TODO: this is just a dummy to continue while i18nHamletFile usage is unclear
        addHtmlMarkdownAlternatives $(ihamletFile "templates/mail/qualificationRenewal.hamlet")        
        

     | otherwise -> do 
        let _letterHead = error "TODO"
        -- makePDF "pdflatex" [] writer woptions pandoc 
        error "TODO"

  