import Testing
import Foundation
@testable import 康康

/// 主页「今日提醒」筛选逻辑(`occurs(on:)`)的纯函数测试。
/// 用固定 Gregorian 日历构造确定日期,避免依赖 `Date.now` / 本机时区。
struct TodayRemindersLogicTests {

    private var cal: Calendar {
        var c = Calendar(identifier: .gregorian)
        c.timeZone = TimeZone(identifier: "Asia/Shanghai")!
        return c
    }

    private func date(_ y: Int, _ mo: Int, _ d: Int) -> Date {
        cal.date(from: DateComponents(year: y, month: mo, day: d, hour: 12))!
    }

    // MARK: - CustomReminder

    @Test func dailyOccursEveryDay() {
        let r = CustomReminder(title: "跑步", frequency: .daily)
        #expect(r.occurs(on: date(2026, 5, 30), calendar: cal))
        #expect(r.occurs(on: date(2026, 1, 1), calendar: cal))
    }

    @Test func disabledNeverOccurs() {
        let r = CustomReminder(title: "跑步", frequency: .daily, enabled: false)
        #expect(!r.occurs(on: date(2026, 5, 30), calendar: cal))
    }

    @Test func weeklyOccursOnlyOnSelectedWeekdays() {
        let d = date(2026, 5, 30)
        let wd = cal.component(.weekday, from: d)
        let other = wd == 1 ? 2 : 1

        let hit = CustomReminder(title: "x", weekdays: [wd], frequency: .weekly)
        #expect(hit.occurs(on: d, calendar: cal))

        let miss = CustomReminder(title: "x", weekdays: [other], frequency: .weekly)
        #expect(!miss.occurs(on: d, calendar: cal))
    }

    @Test func monthlyOccursOnlyOnMatchingDay() {
        let d = date(2026, 5, 30)   // 30 日
        #expect(CustomReminder(title: "x", frequency: .monthly, dayOfMonth: 30).occurs(on: d, calendar: cal))
        #expect(!CustomReminder(title: "x", frequency: .monthly, dayOfMonth: 15).occurs(on: d, calendar: cal))
    }

    @Test func monthlyDay31SkipsShortMonths() {
        // 4 月只有 30 天:选「31 日」的提醒在 4/30 这天不应触发(无 4/31,该月跳过)。
        let apr30 = date(2026, 4, 30)
        let r = CustomReminder(title: "x", frequency: .monthly, dayOfMonth: 31)
        #expect(!r.occurs(on: apr30, calendar: cal))
    }

    @Test func yearlyOccursOnlyOnMatchingMonthAndDay() {
        let d = date(2026, 5, 30)
        #expect(CustomReminder(title: "x", frequency: .yearly, dayOfMonth: 30, month: 5).occurs(on: d, calendar: cal))
        #expect(!CustomReminder(title: "x", frequency: .yearly, dayOfMonth: 30, month: 6).occurs(on: d, calendar: cal))
        #expect(!CustomReminder(title: "x", frequency: .yearly, dayOfMonth: 29, month: 5).occurs(on: d, calendar: cal))
    }

    // MARK: - 多选频率(每日/每周/每月 可同时勾选)

    @Test func multiFrequencyOccursOnAnySelected() {
        // 周一(周二日 = 2)+ 每月15日,两种节奏任一命中即触发。
        let monday = date(2026, 6, 1)   // 2026-06-01 是周一
        let wdMon = cal.component(.weekday, from: monday)
        let r = CustomReminder(title: "x")
        r.frequencies = [.weekly, .monthly]
        r.weekdays = [wdMon]
        r.monthlyDays = [15]
        // 周一但非15号 → 命中(weekly)
        #expect(r.occurs(on: monday, calendar: cal))
        // 15号但非周一(2026-06-15 是周一,换 2026-07-15 是周三)→ 命中(monthly)
        let mid = date(2026, 7, 15)
        #expect(r.occurs(on: mid, calendar: cal))
        // 既非周一也非15号 → 不触发
        #expect(!r.occurs(on: date(2026, 7, 16), calendar: cal))
    }

    @Test func monthlyMultiDayOccursOnEach() {
        let r = CustomReminder(title: "x")
        r.frequencies = [.monthly]
        r.monthlyDays = [1, 15]
        #expect(r.occurs(on: date(2026, 6, 1), calendar: cal))
        #expect(r.occurs(on: date(2026, 6, 15), calendar: cal))
        #expect(!r.occurs(on: date(2026, 6, 10), calendar: cal))
    }

    @Test func legacySingleFrequencyStillReadsThroughFrequenciesFallback() {
        // 旧数据:只设单选 frequency,frequenciesRaw 为空 → frequencies 回退到 [frequency]。
        let r = CustomReminder(title: "x", frequency: .weekly, dayOfMonth: 1)
        r.weekdays = [2]
        #expect(r.frequencies == [.weekly])
        #expect(r.monthlyDays == [1])   // monthDays 空 → 回退 dayOfMonth
    }

    // MARK: - MetricReminder

    @Test func metricReminderOccursOnSelectedWeekday() {
        let d = date(2026, 5, 30)
        let wd = cal.component(.weekday, from: d)
        let other = wd == 1 ? 2 : 1

        #expect(MetricReminder(metricId: "bp", displayName: "血压", weekdays: [wd]).occurs(on: d, calendar: cal))
        #expect(!MetricReminder(metricId: "bp2", displayName: "血压", weekdays: [other]).occurs(on: d, calendar: cal))
    }

    @Test func disabledMetricReminderNeverOccurs() {
        let d = date(2026, 5, 30)
        let wd = cal.component(.weekday, from: d)
        let r = MetricReminder(metricId: "bp", displayName: "血压", weekdays: [wd], enabled: false)
        #expect(!r.occurs(on: d, calendar: cal))
    }
}
