using ClawdDotNet.Core.Tasks;
using Shouldly;

namespace ClawdDotNet.Core.Tests.Tasks;

/// <summary>
/// Die Terminberechnung — Antwort auf B6 (keine Delays, nur „was ist jetzt fällig")
/// und B7 (jeder Termin wird explizit in seiner Zeitzone gedeutet). Reine Funktion,
/// deshalb ohne DB und ohne Uhr-Attrappe: „jetzt" wird direkt übergeben.
/// </summary>
public sealed class TaskScheduleTests
{
    private static DateTime Utc(int y, int mo, int d, int h, int mi)
        => new(y, mo, d, h, mi, 0, DateTimeKind.Utc);

    // ─── Einmalig, ohne Termin ───

    [Fact]
    public void Ohne_Termin_ist_die_Aufgabe_faellig_bis_sie_lief()
    {
        var task = new TaskItem { Id = "t-1", CreatedAt = Utc(2026, 7, 31, 6, 0) };

        TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 7, 0)).ShouldNotBeNull();
        TaskSchedule.DueOccurrence(task with { LastOccurrence = "x" }, Utc(2026, 7, 31, 7, 0)).ShouldBeNull();
    }

    // ─── at: einmaliger Zeitpunkt ───

    [Fact]
    public void Ein_at_Termin_ist_erst_ab_dem_Zeitpunkt_faellig()
    {
        var task = new TaskItem
        {
            Id = "t-1",
            When = new TaskWhen { Kind = TaskWhenKind.At, Value = "2026-07-31T09:00:00", TimeZone = "Europe/Berlin" }
        };

        // 09:00 Berlin (Sommerzeit) = 07:00 UTC
        TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 6, 59)).ShouldBeNull();
        TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 7, 0)).ShouldNotBeNull();
    }

    [Fact]
    public void Ein_at_Termin_feuert_genau_einmal()
    {
        var task = new TaskItem
        {
            Id = "t-1",
            LastOccurrence = "2026-07-31T07:00:00.0000000Z",
            When = new TaskWhen { Kind = TaskWhenKind.At, Value = "2026-07-31T09:00:00", TimeZone = "Europe/Berlin" }
        };

        TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 12, 0)).ShouldBeNull();
    }

    [Fact]
    public void Ein_at_Termin_mit_Z_wird_als_UTC_gedeutet()
    {
        var task = new TaskItem
        {
            Id = "t-1",
            When = new TaskWhen { Kind = TaskWhenKind.At, Value = "2026-07-31T09:00:00Z", TimeZone = "Europe/Berlin" }
        };

        TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 8, 59)).ShouldBeNull();
        TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 9, 0)).ShouldNotBeNull();
    }

    // ─── every: Intervall ───

    [Fact]
    public void Ein_Intervall_wird_erst_nach_Ablauf_faellig()
    {
        var task = new TaskItem
        {
            Id = "t-1",
            CreatedAt = Utc(2026, 7, 31, 7, 0),
            When = new TaskWhen { Kind = TaskWhenKind.Every, Value = "30m", TimeZone = "UTC" }
        };

        TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 7, 20)).ShouldBeNull();
        TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 7, 30)).ShouldNotBeNull();
    }

    [Fact]
    public void Nach_dem_Marker_zaehlt_das_Intervall_neu()
    {
        var task = new TaskItem
        {
            Id = "t-1",
            LastOccurrence = "2026-07-31T07:30:00.0000000Z",
            When = new TaskWhen { Kind = TaskWhenKind.Every, Value = "30m", TimeZone = "UTC" }
        };

        TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 7, 55)).ShouldBeNull();
        TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 8, 0)).ShouldNotBeNull();
    }

    // ─── cron: mit Zeitzone (der Kern von B7) ───

    [Fact]
    public void Ein_Cron_Termin_wird_in_seiner_Zeitzone_gedeutet_Sommer()
    {
        var task = new TaskItem
        {
            Id = "t-1",
            CreatedAt = Utc(2026, 7, 31, 0, 0),
            When = new TaskWhen { Kind = TaskWhenKind.Cron, Value = "0 9 * * *", TimeZone = "Europe/Berlin" }
        };

        // 09:00 Berlin im Sommer (CEST, UTC+2) = 07:00 UTC
        var occ = TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 7, 5));
        occ.ShouldBe("2026-07-31T07:00:00.0000000Z");
    }

    [Fact]
    public void Derselbe_Cron_liegt_im_Winter_auf_einer_anderen_UTC_Stunde()
    {
        var task = new TaskItem
        {
            Id = "t-1",
            CreatedAt = Utc(2026, 1, 15, 0, 0),
            When = new TaskWhen { Kind = TaskWhenKind.Cron, Value = "0 9 * * *", TimeZone = "Europe/Berlin" }
        };

        // 09:00 Berlin im Winter (CET, UTC+1) = 08:00 UTC — genau das, was B7 verlangt.
        var occ = TaskSchedule.DueOccurrence(task, Utc(2026, 1, 15, 8, 5));
        occ.ShouldBe("2026-01-15T08:00:00.0000000Z");
    }

    [Fact]
    public void Vor_der_geplanten_Zeit_ist_der_Cron_nicht_faellig()
    {
        var task = new TaskItem
        {
            Id = "t-1",
            CreatedAt = Utc(2026, 7, 31, 5, 0),
            When = new TaskWhen { Kind = TaskWhenKind.Cron, Value = "0 9 * * *", TimeZone = "Europe/Berlin" }
        };

        // 06:00 UTC = 08:00 Berlin, vor der 09:00-Marke
        TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 6, 0)).ShouldBeNull();
    }

    [Fact]
    public void Eine_frische_Cron_Aufgabe_holt_keinen_Termin_von_vor_ihrer_Anlage_nach()
    {
        var task = new TaskItem
        {
            Id = "t-1",
            CreatedAt = Utc(2026, 7, 31, 8, 0), // nach der heutigen 09:00-Berlin-Marke (07:00 UTC)
            When = new TaskWhen { Kind = TaskWhenKind.Cron, Value = "0 9 * * *", TimeZone = "Europe/Berlin" }
        };

        // Jetzt 10:00 UTC — die heutige 09:00-Marke lag vor der Anlage, also nicht nachholen.
        TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 10, 0)).ShouldBeNull();
    }

    [Fact]
    public void Derselbe_Cron_Termin_feuert_nicht_zweimal()
    {
        var task = new TaskItem
        {
            Id = "t-1",
            LastOccurrence = "2026-07-31T07:00:00.0000000Z",
            When = new TaskWhen { Kind = TaskWhenKind.Cron, Value = "0 9 * * *", TimeZone = "Europe/Berlin" }
        };

        // Später am selben Tag: der 09:00-Termin ist schon abgehakt.
        TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 10, 0)).ShouldBeNull();

        // Der nächste Tag ist wieder fällig.
        TaskSchedule.DueOccurrence(task, Utc(2026, 8, 1, 7, 5)).ShouldBe("2026-08-01T07:00:00.0000000Z");
    }

    [Fact]
    public void Ein_kaputter_Cron_Ausdruck_kippt_den_Scanner_nicht()
    {
        var task = new TaskItem
        {
            Id = "t-1",
            When = new TaskWhen { Kind = TaskWhenKind.Cron, Value = "kein cron", TimeZone = "UTC" }
        };

        Should.NotThrow(() => TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 9, 0)))
            .ShouldBeNull();
    }

    /// <summary>
    /// Früher fiel eine unbekannte Zone still auf UTC zurück. Das ist der schlechtere
    /// Fehler: Ein Task für 09:00 Ortszeit lief im Sommer um 07:00, ohne dass irgendwo
    /// etwas auffiel. Jetzt feuert er gar nicht — und der Scanner meldet einmal, warum.
    /// </summary>
    [Fact]
    public void Eine_unbekannte_Zeitzone_laesst_den_Termin_nicht_faellig_werden()
    {
        var task = new TaskItem
        {
            Id = "t-1",
            CreatedAt = Utc(2026, 7, 31, 0, 0),
            When = new TaskWhen { Kind = TaskWhenKind.Cron, Value = "0 9 * * *", TimeZone = "Phantasie/Ort" }
        };

        TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 9, 5)).ShouldBeNull();
        TaskSchedule.UnresolvableTimeZone(task).ShouldBe("Phantasie/Ort");
    }

    /// <summary>
    /// Der Kern der Portierung: Dieselbe Zeitzone, zwei Schreibweisen. Eine Task-Datei,
    /// die unter Windows entstanden ist, muss unter Linux dieselbe Feuerzeit ergeben —
    /// und umgekehrt.
    /// </summary>
    [Theory]
    [InlineData("Europe/Berlin")]
    [InlineData("W. Europe Standard Time")]
    public void Beide_Schreibweisen_derselben_Zone_ergeben_dieselbe_Feuerzeit(string tz)
    {
        var task = new TaskItem
        {
            Id = "t-1",
            CreatedAt = Utc(2026, 7, 31, 0, 0),
            When = new TaskWhen { Kind = TaskWhenKind.Cron, Value = "0 9 * * *", TimeZone = tz }
        };

        TaskSchedule.UnresolvableTimeZone(task).ShouldBeNull();

        // 09:00 Ortszeit im Sommer (MESZ, UTC+2) = 07:00 UTC.
        TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 9, 5))
            .ShouldBe("2026-07-31T07:00:00.0000000Z");
    }

    [Fact]
    public void Keine_Zeitzonenangabe_bedeutet_weiterhin_UTC()
    {
        var task = new TaskItem
        {
            Id = "t-1",
            CreatedAt = Utc(2026, 7, 31, 0, 0),
            When = new TaskWhen { Kind = TaskWhenKind.Cron, Value = "0 9 * * *", TimeZone = "" }
        };

        TaskSchedule.UnresolvableTimeZone(task).ShouldBeNull();
        TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 9, 5))
            .ShouldBe("2026-07-31T09:00:00.0000000Z");
    }
}
