package com.ffii.fpsms.modules.common.mail.service

import com.ffii.fpsms.modules.common.SettingNames
import com.ffii.fpsms.modules.common.mail.pojo.MailRequest
import com.ffii.fpsms.modules.common.mail.web.models.WorkHourRecords
import com.ffii.fpsms.modules.settings.service.SettingsService
import com.ffii.fpsms.modules.user.service.UserService
import jakarta.mail.internet.InternetAddress
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.YearMonth
import java.time.format.DateTimeFormatter

data class TableRow(
    val staffId: String,
    val name: String,
    val missingDates: MutableList<LocalDate>,
)

@Service
open class MailReminderService(
    val mailService: MailService,
    val userService: UserService,
    val settingsService: SettingsService,
) {
    protected val logger: Log = LogFactory.getLog(javaClass)
    private val FULLTIME = "FT"
    private val PARTTIME = "PT"
    private val dateFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")

    private fun isSettingsConfigValid(): Boolean {
        try {
            val username = settingsService.findByName(SettingNames.MAIL_SMTP_USERNAME).orElseThrow().value
            val pw = settingsService.findByName(SettingNames.MAIL_SMTP_USERNAME).orElseThrow().value
            return !username.isNullOrEmpty() && !pw.isNullOrEmpty()
        } catch (e: Error) {
            return false
        }
    }
    private fun createHTMLTable(naughtyList: MutableList<TableRow>): String {
        val tableStarter = StringBuilder("  <table width='100%' border='1' align='center'> ")
        val header = StringBuilder(
            " <tr> "
                    + " <th>StaffId</th> "
                    + " <th>Name</th> "
                    + " <th>Missing Dates</th> "
                    + " </tr> "
            )
        tableStarter.append(header)
        for (per in naughtyList) {
            tableStarter.append(
                " <tr> "
                + " <td>${per.staffId}</td>"
                + " <td>${per.name}</td>"
                + " <td>${per.missingDates.joinToString(", ")}</td>"
                + " </tr> "
            )
        }
        val footer = StringBuilder(" </table> ")
        return tableStarter.toString()
    }

    private fun createEmailRequest(content: String, emailTo: List<String>) {
//        val subject = settingsService.findByName(SettingNames.TIMESHEET_MAIL_SUBJECT).orElseThrow().value
//        val template = settingsService.findByName(SettingNames.TIMESHEET_MAIL_TEMPLATE).orElseThrow().value
//        val cc = settingsService.findByName(SettingNames.TIMESHEET_MAIL_CC).orElseThrow().value.split(",")
//        val bcc = settingsService.findByName(SettingNames.TIMESHEET_MAIL_BCC).orElseThrow().value.split(",")
        val mailRequest = MailRequest.Builder()
//            .subject(subject)
//            .templateContent(template)
            .args(
                mapOf(
                    Pair("date", content),
                )
            )
            .addTo(emailTo)
//            .addCc(cc)
//            .addBcc(bcc)
            .build()
        val mailRequestList = mutableListOf<MailRequest>()
        mailRequestList += mailRequest
        mailService.send(mailRequestList)
    }
}