-- 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.ExpireQualification where

import Import 

-- 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


data LetterExpireQualification = LetterExpireQualification
    { leqHolderCFN :: CryptoFileNameUser
    , leqHolderID  :: UserId
    , leqHolderDN  :: UserDisplayName
    , leqHolderSN  :: UserSurname
    , leqExpiry    :: Maybe Day
    , leqId        :: QualificationId
    , leqName      :: Text            
    , leqShort     :: Text       
    , leqSchool    :: SchoolId
    , leqUrl       :: Maybe Text
    }
  deriving (Eq, Show)


instance MDLetter LetterExpireQualification where  
  encryptPDFfor     _ = NoPassword
  getLetterKind     _ = Din5008
  getLetterEnvelope _ = 'e'
  getMailSubject    l = SomeMessage $ MsgMailSubjectQualificationExpired $ leqShort l

  getTemplate LetterExpireQualification{leqShort="F"} 
                = decodeUtf8 $(Data.FileEmbed.embedFile "templates/letter/fraport_f_expiry.md")
  getTemplate _ = decodeUtf8 $(Data.FileEmbed.embedFile "templates/letter/fraport_generic_expiry.md")
  

  letterMeta LetterExpireQualification{..} DateTimeFormatter{ format } lang Entity{entityKey=rcvrId, entityVal=User{userDisplayName}} = 
    let isSupervised = rcvrId /= leqHolderID
    in mkMeta $
      guardMonoid isSupervised
      [ toMeta "supervisor" userDisplayName      
      ] <>
      [ toMeta "lang"           lang
      , toMeta "licencename"    leqName
      , toMeta "licenceshort"   leqShort
      , toMeta "licenceholder"  leqHolderDN
      , toMeta "subject-meta"   leqHolderDN
      , mbMeta "expiry"         (format SelFormatDate <$> leqExpiry)
      , mbMeta "licence-url"    leqUrl
      , toMeta "de-opening" $ bool ("Guten Tag " <> leqHolderDN <> ",") "Sehr geehrte Damen und Herren," isSupervised
      , toMeta "en-opening" $ bool ("Dear "      <> leqHolderDN <> ",") "Dear supervisor,"               isSupervised
      , toMeta "de-subject" $ "Entzug \"" <> leqShort <> "\" (" <> leqName <> ")"
      , toMeta "en-subject" $ case leqShort of
                                "F" -> "Revocation of apron driving license"
                                _   -> "Revocation of licence \"" <> leqShort <> "\" (" <> leqName <> ")"
      ] 

  getPJId LetterExpireQualification{..} =
    PrintJobIdentification
      { pjiName             = "Expiry"
      , pjiApcAcknowledge   = "ex-" <> toPathPiece leqHolderCFN
      , pjiRecipient        = Nothing       -- to be filled later
      , pjiSender           = Nothing
      , pjiCourse           = Nothing
      , pjiQualification    = Just leqId
      , pjiLmsUser          = Nothing
      , pjiFileName         = "expire_" <> CI.original (unSchoolKey leqSchool) <> "-" <> leqShort <> "_" <> leqHolderSN 
        -- 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])
      }