package app.closer.domain

import java.util.Calendar

/**
 * Resolves which daily mode to use for the current day.
 *
 * Day-of-week defaults are deterministic so the same mode always shows on
 * the same calendar day. A ~10% wildcard override keeps things surprising.
 */
object DailyModeResolver {

    data class DailyMode(
        val id: String,
        val title: String,
        val subtitle: String,
        val actionCopy: String,
        val modeTag: String
    )

    private val MODES = mapOf(
        "soft_monday" to DailyMode(
            id = "soft_monday",
            title = "Soft Monday",
            subtitle = "Tiny, easy, still us.",
            actionCopy = "Tiny mission: make tonight easier on purpose.",
            modeTag = "mode_soft_monday"
        ),
        "snack_mission" to DailyMode(
            id = "snack_mission",
            title = "Snack Mission",
            subtitle = "The snack is part of the plot.",
            actionCopy = "Snack mission: find one treat worth sharing.",
            modeTag = "mode_snack_mission"
        ),
        "no_phone_moment" to DailyMode(
            id = "no_phone_moment",
            title = "No-Phone Moment",
            subtitle = "Just a tiny bit of us.",
            actionCopy = "No-phone moment: 10 minutes, just us.",
            modeTag = "mode_no_phone_moment"
        ),
        "laugh_reset" to DailyMode(
            id = "laugh_reset",
            title = "Laugh Reset",
            subtitle = "Pressure off. Weird on.",
            actionCopy = "Laugh reset: tell today's nonsense dramatically.",
            modeTag = "mode_laugh_reset"
        ),
        "flirty_friday" to DailyMode(
            id = "flirty_friday",
            title = "Flirty Friday",
            subtitle = "A little spark, no pressure.",
            actionCopy = "Tiny mission: send one flirty text.",
            modeTag = "mode_flirty_friday"
        ),
        "weekend_side_quest" to DailyMode(
            id = "weekend_side_quest",
            title = "Weekend Side Quest",
            subtitle = "Pick today's tiny mission.",
            actionCopy = "Side quest: do one tiny thing outside the usual.",
            modeTag = "mode_weekend_side_quest"
        ),
        "tiny_date_night" to DailyMode(
            id = "tiny_date_night",
            title = "Tiny Date Night",
            subtitle = "Small plan. Big maybe.",
            actionCopy = "Tiny mission: make one ordinary thing feel like a date.",
            modeTag = "mode_tiny_date_night"
        ),
        "couple_lore_day" to DailyMode(
            id = "couple_lore_day",
            title = "Couple Lore Day",
            subtitle = "Add to the us-history.",
            actionCopy = "Couple lore: name this tiny moment.",
            modeTag = "mode_couple_lore_day"
        ),
        "low_battery_day" to DailyMode(
            id = "low_battery_day",
            title = "Low Battery Day",
            subtitle = "Easy counts today.",
            actionCopy = "Tiny mission: lower the bar together.",
            modeTag = "mode_low_battery_day"
        ),
        "wildcard" to DailyMode(
            id = "wildcard",
            title = "Wildcard",
            subtitle = "The app has opinions.",
            actionCopy = "Wildcard mission: say yes to one tiny weird idea.",
            modeTag = "mode_wildcard"
        ),
    )

    private val DOW_DEFAULTS = mapOf(
        Calendar.MONDAY to "soft_monday",
        Calendar.TUESDAY to "snack_mission",
        Calendar.WEDNESDAY to "no_phone_moment",
        Calendar.THURSDAY to "laugh_reset",
        Calendar.FRIDAY to "flirty_friday",
        Calendar.SATURDAY to "weekend_side_quest",
        Calendar.SUNDAY to "tiny_date_night",
    )

    fun resolve(calendar: Calendar = Calendar.getInstance()): DailyMode {
        val dow = calendar.get(Calendar.DAY_OF_WEEK)
        // ~10% wildcard using day-of-year so it's repeatable within the same day
        val doy = calendar.get(Calendar.DAY_OF_YEAR)
        val modeId = if (doy % 10 == 3) {
            "wildcard"
        } else {
            DOW_DEFAULTS[dow] ?: "tiny_date_night"
        }
        return MODES[modeId] ?: MODES["tiny_date_night"]!!
    }

    fun getMode(id: String): DailyMode? = MODES[id]

    fun allModes(): Collection<DailyMode> = MODES.values
}
