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

#nullable disable

namespace Backend.Migrations;

[DbContext(typeof(Data.AwardsDbContext))]
[Migration("20260629220000_SeedDemoArchivedWinners2025")]
public partial class SeedDemoArchivedWinners2025 : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.Sql(
            """
            DO $vtsa_archive_2025$
            BEGIN
            IF EXISTS (
                SELECT 1 FROM "Seasons"
                WHERE "Year" = 2025 AND "IsDemo" = FALSE
            ) THEN
                RAISE NOTICE 'Echte 2025-Season vorhanden; Demo-Archivgewinner werden 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$;
            """
        );
    }

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