using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Backend.Migrations;

[DbContext(typeof(Data.AwardsDbContext))]
[Migration("20260629230000_SeedDemoArchivedWinners2025Force")]
public partial class SeedDemoArchivedWinners2025Force : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        // The original SeedDemoArchivedWinners2025 migration skipped seeding whenever
        // any non-demo 2025 season row existed. Production carries empty 2025/2024 season
        // shells (no published winners), so the archive stayed empty. Guard precisely on
        // real published winner results instead, so the demo archive is populated unless
        // genuine 2025 winners exist.
        migrationBuilder.Sql(
            """
            DO $vtsa_archive_2025_force$
            BEGIN
            IF EXISTS (
                SELECT 1
                FROM "Results" r
                JOIN "Seasons" s ON s."Id" = r."SeasonId"
                WHERE s."Year" = 2025 AND s."WinnersPublishedAt" IS NOT NULL
            ) THEN
                RAISE NOTICE 'Echte veroeffentlichte 2025-Gewinner vorhanden; Demo-Archiv wird nicht eingespielt.';
                RETURN;
            END IF;

            INSERT INTO "ArchivedWinners" (
                "Year", "Category", "Subcategory", "WinnerName", "WinnerUrl", "CreatedAt"
            )
            VALUES
                (2025, 'Gaming',    'Hidden Star',   'Archiv Aster', 'https://twitch.tv/archiv_aster',               TIMESTAMPTZ '2025-06-22 10:05:00+00'),
                (2025, 'Gaming',    'Rising Star',   'Archiv Beryl', 'https://twitch.tv/archiv_beryl',               TIMESTAMPTZ '2025-06-22 10:06:00+00'),
                (2025, 'Gaming',    'Shining Star',  'Archiv Coda',  'https://clips.twitch.tv/demo-archiv-coda',     TIMESTAMPTZ '2025-06-22 10:07:00+00'),
                (2025, 'Music',     'Hidden Star',   'Archiv Drift', 'https://twitch.tv/archiv_drift',               TIMESTAMPTZ '2025-06-22 10:08:00+00'),
                (2025, 'Music',     'Rising Star',   'Archiv Elara', 'https://youtu.be/demo-archiv-elara',           TIMESTAMPTZ '2025-06-22 10:09:00+00'),
                (2025, 'Music',     'Shining Star',  'Archiv Finch', 'https://clips.twitch.tv/demo-archiv-finch',    TIMESTAMPTZ '2025-06-22 10:10:00+00'),
                (2025, 'Community', 'Hidden Star',   'Archiv Lyra',  'https://twitch.tv/archiv_lyra',                TIMESTAMPTZ '2025-06-22 10:11:00+00'),
                (2025, 'Community', 'Rising Star',   'Archiv Muse',  'https://youtu.be/demo-archiv-muse',            TIMESTAMPTZ '2025-06-22 10:12:00+00'),
                (2025, 'Community', 'Shining Star',  'Archiv Nia',   'https://clips.twitch.tv/demo-archiv-nia',      TIMESTAMPTZ '2025-06-22 10:13:00+00')
            ON CONFLICT ("Year", "Category", "Subcategory") DO NOTHING;

            PERFORM setval(pg_get_serial_sequence('"ArchivedWinners"', 'Id'), COALESCE((SELECT MAX("Id") FROM "ArchivedWinners"), 1));
            END;
            $vtsa_archive_2025_force$;
            """
        );
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.Sql(
            """
            DELETE FROM "ArchivedWinners"
            WHERE "Year" = 2025
              AND "Category" IN ('Gaming', 'Music', 'Community')
              AND "WinnerName" LIKE 'Archiv %';
            """
        );
    }
}
