namespace SVSim.UnitTests.Services;

public class JstPeriodTests
{
    private static DateTimeOffset Jst(int y, int m, int d, int h, int min, int s) =>
        new DateTimeOffset(y, m, d, h, min, s, TimeSpan.FromHours(9));

    [Test]
    public void DayKey_uses_jst_with_02_00_anchor()
    {
        // 01:59:59 JST on May 27 belongs to the May 26 "day".
        Assert.That(SVSim.EmulatedEntrypoint.Services.JstPeriod.DayKey(Jst(2026, 5, 27, 1, 59, 59)),
            Is.EqualTo("day:2026-05-26"));

        // 02:00:00 JST on May 27 belongs to the May 27 "day".
        Assert.That(SVSim.EmulatedEntrypoint.Services.JstPeriod.DayKey(Jst(2026, 5, 27, 2, 0, 0)),
            Is.EqualTo("day:2026-05-27"));
    }

    [Test]
    public void WeekKey_is_iso8601_monday_anchored()
    {
        // 2026-05-25 (Monday) 02:00 JST → ISO week 2026-W22.
        Assert.That(SVSim.EmulatedEntrypoint.Services.JstPeriod.WeekKey(Jst(2026, 5, 25, 2, 0, 0)),
            Is.EqualTo("week:2026-W22"));

        // The previous Sunday's late evening still belongs to W21 (week reset is Monday 02:00 JST).
        Assert.That(SVSim.EmulatedEntrypoint.Services.JstPeriod.WeekKey(Jst(2026, 5, 25, 1, 59, 59)),
            Is.EqualTo("week:2026-W21"));
    }

    [Test]
    public void MonthKey_uses_jst_day_anchor_for_month_rollover()
    {
        // 01:59 JST on June 1 still belongs to May (day-boundary is 02:00 JST).
        Assert.That(SVSim.EmulatedEntrypoint.Services.JstPeriod.MonthKey(Jst(2026, 6, 1, 1, 59, 0)),
            Is.EqualTo("month:2026-05"));

        Assert.That(SVSim.EmulatedEntrypoint.Services.JstPeriod.MonthKey(Jst(2026, 6, 1, 2, 0, 0)),
            Is.EqualTo("month:2026-06"));
    }

    [Test]
    public void AllPeriods_returns_day_week_month_all_time()
    {
        var t = Jst(2026, 5, 27, 12, 0, 0);
        var periods = SVSim.EmulatedEntrypoint.Services.JstPeriod.AllPeriods(t);
        Assert.That(periods, Is.EquivalentTo(new[] {
            "day:2026-05-27", "week:2026-W22", "month:2026-05", "all-time",
        }));
    }
}
