package com.ffii.fpsms.modules.common.alert

import org.springframework.boot.context.properties.ConfigurationProperties

@ConfigurationProperties(prefix = "scheduler.sync-alert")
data class SchedulerSyncAlertProperties(
    /** When false, checks still run but SMS is not sent (logged only). */
    val enabled: Boolean = false,
    /** Cron for the watchdog that evaluates sync health (default: every 15 minutes). */
    val checkCron: String = "0 */15 * * * *",
    val sms: SmsProperties = SmsProperties(),
    val email: EmailAlertProperties = EmailAlertProperties(),
    val do1: Do1AlertProperties = Do1AlertProperties(),
    val presence: PresenceAlertProperties = PresenceAlertProperties(),
    val poLine: PoLineAlertProperties = PoLineAlertProperties(),
)

data class SmsProperties(
    /** When false, no SMS/WhatsApp alerts (email-only). */
    val enabled: Boolean = false,
    /** twilio | log (dev: log message only, no HTTP) */
    val provider: String = "log",
    /** sms or whatsapp (Twilio WhatsApp uses whatsapp:+E164 on To/From). */
    val channel: String = "sms",
    val accountSid: String = "",
    val authToken: String = "",
    val fromNumber: String = "",
    /** Comma-separated E.164 numbers, e.g. +85291234567 (whatsapp prefix added when channel=whatsapp). */
    val toNumbers: String = "",
    /**
     * Twilio Content Template SID (WhatsApp business-initiated messages).
     * When set, sends ContentSid + ContentVariables instead of Body.
     * Template should use {{1}}=job (DO1/PO/…) and {{2}}=alert detail.
     */
    val contentSid: String = "",
    val contentDetailMaxLength: Int = 200,
    /**
     * Maps to Twilio ContentVariables `"1"` (template {{1}}).
     * job | date (d/M, e.g. 17/6) | time (3pm) | detail | summary (job + code + detail)
     */
    val contentVar1: String = "job",
    /** Maps to ContentVariables `"2"`. Same options as [contentVar1]. */
    val contentVar2: String = "detail",
)

data class EmailAlertProperties(
    /** Send sync alerts by email (uses DB MAIL.smtp.* — Office 365). */
    val enabled: Boolean = false,
    /** Comma-separated recipients, e.g. vluk@2fi-solutions.com.hk,ops@example.com */
    val toAddresses: String = "",
    /** Subject prefix; full subject is prefix + job + code in brackets. */
    val subjectPrefix: String = "FPSMS M18 sync alert",
)

data class Do1AlertProperties(
    /** Warn when recordsProcessed is below this (typical full day ~800+). */
    val minRecordsProcessed: Int = 400,
    /** Minutes after today's DO1 schedule time before evaluating DO1 rules. */
    val graceMinutesAfterSchedule: Int = 30,
    val alertOnFailed: Boolean = true,
    val alertOnZeroRecords: Boolean = true,
    val alertOnLineFailures: Boolean = true,
)

/** PO / DO2 / master-data: alert if no SUCCESS log by schedule time + grace. */
data class PresenceAlertProperties(
    val graceMinutesAfterSchedule: Int = 60,
)

/** Purchase order line sync failures from [m18_data_log] (one email per PO code per day). */
data class PoLineAlertProperties(
    val enabled: Boolean = true,
)
