-- SPDX-FileCopyrightText: 2023 Steffen Jost <jost@tcs.ifi.lmu.de>
--
-- SPDX-License-Identifier: AGPL-3.0-or-later

{-# OPTIONS_GHC -fno-warn-unused-top-binds #-}

module Utils.Print.RenewQualification where

import Import 
import Text.Hamlet

-- import           Data.Char as Char
-- import qualified Data.Text as Text
import qualified Data.CaseInsensitive as CI

import Data.FileEmbed (embedFile)

import Utils.Print.Letters
import Handler.Utils.Widgets (nameHtml) -- , nameHtml')


data LetterRenewQualificationF = LetterRenewQualificationF
    { lmsLogin      :: LmsIdent
    , lmsPin        :: Text
    , qualHolderID  :: UserId
    , qualHolderDN  :: UserDisplayName
    , qualHolderSN  :: UserSurname
    , qualExpiry    :: Day
    , qualId        :: QualificationId
    , qualName      :: Text
    , qualShort     :: Text
    , qualSchool    :: SchoolId
    , qualDuration  :: Maybe Int
    , isReminder    :: Bool
    }
  deriving (Eq, Show)


-- this datatype is specific to this letter only, and just to avoid code duplication for derived data or constants
data LetterRenewQualificationFData = LetterRenewQualificationFData { lmsUrl, lmsUrlLogin, lmsIdent :: Text } 
  deriving (Eq, Show)

letterRenewalQualificationFData :: LetterRenewQualificationF -> LetterRenewQualificationFData
letterRenewalQualificationFData LetterRenewQualificationF{lmsLogin} = LetterRenewQualificationFData{..}
  where 
    lmsUrl      = "https://drive.fraport.de"
    lmsUrlLogin = lmsUrl <> "/?login=" <> lmsIdent
    lmsIdent    = getLmsIdent lmsLogin    


instance MDLetter LetterRenewQualificationF where  
  encryptPDFfor     _ = PasswordUnderling
  getLetterKind     _ = PinLetter
  getLetterEnvelope _ = 'f' -- maybe 'q' (Char.toLower . fst) $ Text.uncons (qualShort l)
  getTemplate       _ = decodeUtf8 $(Data.FileEmbed.embedFile "templates/letter/fraport_renewal.md")
  getMailSubject    l = SomeMessage $ MsgMailSubjectQualificationRenewal $ qualShort l
  getMailBody l@LetterRenewQualificationF{..} = Just $ \DateTimeFormatter{ format } ->
    let LetterRenewQualificationFData{..} = letterRenewalQualificationFData l
    in $(ihamletFile "templates/mail/body/qualificationRenewal.hamlet")

  letterMeta l@LetterRenewQualificationF{..} DateTimeFormatter{ format } lang Entity{entityKey=rcvrId, entityVal=User{userDisplayName}} = 
    let LetterRenewQualificationFData{..} = letterRenewalQualificationFData l 
        isSupervised = rcvrId /= qualHolderID
    in mkMeta $
      guardMonoid isSupervised
      [ toMeta "supervisor" userDisplayName
      , toMeta "de-opening" ("Sehr geehrte Damen und Herren,"::Text)
      , toMeta "en-opening" ("Dear Sir or Madam,"::Text)
      ] <>
      guardMonoid isReminder
      [ toMeta "reminder"       ("reminder"::Text)
      ] <>
      [ toMeta "lang"           lang
      , toMeta "login"          lmsIdent
      , toMeta "pin"            lmsPin
      , toMeta "examinee"       qualHolderDN
      , toMeta "subject-meta"   qualHolderDN
      , toMeta "expiry"         (format SelFormatDate qualExpiry)
      , mbMeta "validduration"  (show <$> qualDuration)
      , toMeta "url-text"       lmsUrl
      , toMeta "url"            lmsUrlLogin

      ] 

  getPJId LetterRenewQualificationF{..} =
    PrintJobIdentification
      { pjiName             = bool "Renewal" "Renewal Reminder" isReminder
      , pjiApcAcknowledge   = "lms-" <> getLmsIdent lmsLogin
      , pjiRecipient        = Nothing       -- to be filled later
      , pjiSender           = Nothing
      , pjiCourse           = Nothing
      , pjiQualification    = Just qualId
      , pjiLmsUser          = Just lmsLogin
      , pjiFileName         = "renew_" <> CI.original (unSchoolKey qualSchool) <> "-" <> qualShort <> "_" <> qualHolderSN 
        -- let nameRecipient = abbrvName <$> recipient
        --     nameSender    = abbrvName <$> sender
        --     nameCourse    = CI.original . courseShorthand        <$> course
        --     nameQuali     = CI.original . qualificationShorthand <$> quali
        -- in .. = T.replace " " "-" (T.intercalate "_" . catMaybes $ [Just printJobName, nameQuali, nameCourse, nameSender, nameRecipient])
      }