using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Backend.Migrations;

public partial class AdjustDemoSubcategoriesAndArchive : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.Sql(
            """
            DO $vtsa_adjust$
            BEGIN
            IF EXISTS (
                SELECT 1
                FROM "Seasons"
                WHERE "Year" IN (2024, 2025, 2026)
                  AND "Id" NOT IN (999, 1000, 1001)
            ) THEN
                RAISE NOTICE 'Existing non-demo season years detected; skipping demo archive reseed.';
                RETURN;
            END IF;

            DELETE FROM "RiskFlags" WHERE "SeasonId" IN (999, 1000, 1001);
            DELETE FROM "Seasons" WHERE "Id" IN (999, 1000, 1001) AND "IsDemo" = TRUE;
            DELETE FROM "StreamerIdentities" WHERE "Id" BETWEEN 3001 AND 3020;

            INSERT INTO "Seasons" (
                "Id", "Year", "Name", "IsDemo", "IsCurrent", "IsCommunityOnly", "CurrentPhase",
                "NominationStartsAt", "NominationEndsAt", "VotingStartsAt", "VotingEndsAt",
                "ReviewStartsAt", "ReviewEndsAt", "ShowDate", "ShowStartsAt",
                "WinnersPublishedAt", "WinnersPublishedByTwitchId", "SubcategoryTemplatesJson", "WorkflowRulesJson"
            )
            VALUES
                (999, 2024, 'VTuber Star Awards 2024 Demo Archiv', TRUE, FALSE, FALSE, 'Archiv',
                 DATE '2024-04-01', DATE '2024-04-28', DATE '2024-05-10', DATE '2024-05-24',
                 DATE '2024-04-29', DATE '2024-05-09', DATE '2024-06-22', TIME '20:00',
                 TIMESTAMPTZ '2024-06-23 10:00:00+00', 'demo_owner',
                 '[{"name":"Hidden Star","slug":"hidden-star","sortOrder":1,"viewerRangeMin":1,"viewerRangeMax":20},{"name":"Rising Star","slug":"rising-star","sortOrder":2,"viewerRangeMin":21,"viewerRangeMax":60},{"name":"Shining Star","slug":"shining-star","sortOrder":3,"viewerRangeMin":61,"viewerRangeMax":null}]',
                 '[]'),
                (1000, 2025, 'VTuber Star Awards 2025 Demo Archiv', TRUE, FALSE, FALSE, 'Archiv',
                 DATE '2025-04-01', DATE '2025-04-28', DATE '2025-05-10', DATE '2025-05-24',
                 DATE '2025-04-29', DATE '2025-05-09', DATE '2025-06-21', TIME '20:00',
                 TIMESTAMPTZ '2025-06-22 10:00:00+00', 'demo_owner',
                 '[{"name":"Hidden Star","slug":"hidden-star","sortOrder":1,"viewerRangeMin":1,"viewerRangeMax":20},{"name":"Rising Star","slug":"rising-star","sortOrder":2,"viewerRangeMin":21,"viewerRangeMax":60},{"name":"Shining Star","slug":"shining-star","sortOrder":3,"viewerRangeMin":61,"viewerRangeMax":null}]',
                 '[]'),
                (1001, 2026, 'VTuber Star Awards 2026 Demo', TRUE, TRUE, FALSE, 'Voting',
                 DATE '2026-05-18', DATE '2026-06-16', DATE '2026-06-24', DATE '2026-07-19',
                 DATE '2026-06-17', DATE '2026-06-23', DATE '2026-08-08', TIME '20:00',
                 NULL, NULL,
                 '[{"name":"Hidden Star","slug":"hidden-star","sortOrder":1,"viewerRangeMin":1,"viewerRangeMax":20},{"name":"Rising Star","slug":"rising-star","sortOrder":2,"viewerRangeMin":21,"viewerRangeMax":60},{"name":"Shining Star","slug":"shining-star","sortOrder":3,"viewerRangeMin":61,"viewerRangeMax":null}]',
                 '[]');

            WITH tiers(ord, name, slug, min_viewers, max_viewers) AS (
                VALUES
                    (1, 'Hidden Star', 'hidden-star', 1, 20),
                    (2, 'Rising Star', 'rising-star', 21, 60),
                    (3, 'Shining Star', 'shining-star', 61, NULL)
            ),
            active_groups(ord, name, slug) AS (
                VALUES
                    (1, 'Gaming', 'gaming'),
                    (2, 'Music', 'music'),
                    (3, 'Art & Design', 'art-design'),
                    (4, 'Community', 'community')
            ),
            archive_2025_groups(ord, name, slug) AS (
                VALUES
                    (1, 'Gaming', 'gaming'),
                    (2, 'Music', 'music'),
                    (3, 'Community', 'community')
            ),
            archive_2024_groups(ord, name, slug) AS (
                VALUES
                    (1, 'Gaming', 'gaming'),
                    (2, 'Music', 'music')
            )
            INSERT INTO "Categories" (
                "Id", "SeasonId", "GroupName", "Name", "Slug", "Description", "SortOrder", "MaxNomineesPerUser", "ViewerRangeMin", "ViewerRangeMax"
            )
            SELECT 1100 + ((g.ord - 1) * 3) + t.ord, 1001, g.name, t.name, g.slug || '-' || t.slug,
                   g.name || '-Creator im ' || t.name || '-Tier.', ((g.ord - 1) * 30) + (t.ord * 10), 2, t.min_viewers, t.max_viewers
            FROM active_groups g CROSS JOIN tiers t
            UNION ALL
            SELECT 1200 + ((g.ord - 1) * 3) + t.ord, 1000, g.name, t.name, 'archive-2025-' || g.slug || '-' || t.slug,
                   'Archiv-Unterkategorie.', ((g.ord - 1) * 30) + (t.ord * 10), 2, t.min_viewers, t.max_viewers
            FROM archive_2025_groups g CROSS JOIN tiers t
            UNION ALL
            SELECT 1300 + ((g.ord - 1) * 3) + t.ord, 999, g.name, t.name, 'archive-2024-' || g.slug || '-' || t.slug,
                   'Archiv-Unterkategorie.', ((g.ord - 1) * 30) + (t.ord * 10), 2, t.min_viewers, t.max_viewers
            FROM archive_2024_groups g CROSS JOIN tiers t;

            INSERT INTO "StreamerIdentities" ("Id", "Platform", "Login", "NormalizedKey", "DisplayName", "ProfileUrl", "LastResolvedAt")
            VALUES
                (3001, 'Twitch', 'aki_lumina', 'twitch:aki_lumina', 'Aki Lumina', 'https://twitch.tv/aki_lumina', TIMESTAMPTZ '2026-06-27 12:00:00+00'),
                (3002, 'Twitch', 'mira_orbit', 'twitch:mira_orbit', 'Mira Orbit', 'https://twitch.tv/mira_orbit', TIMESTAMPTZ '2026-06-27 12:02:00+00'),
                (3003, 'Twitch', 'nova_nym', 'twitch:nova_nym', 'Nova Nym', 'https://twitch.tv/nova_nym', TIMESTAMPTZ '2026-06-27 12:04:00+00'),
                (3004, 'Twitch', 'luna_koi', 'twitch:luna_koi', 'Luna Koi', 'https://twitch.tv/luna_koi', TIMESTAMPTZ '2026-06-27 12:06:00+00'),
                (3005, 'Twitch', 'runa_bits', 'twitch:runa_bits', 'Runa Bits', 'https://twitch.tv/runa_bits', TIMESTAMPTZ '2026-06-27 12:08:00+00'),
                (3006, 'Twitch', 'sora_slate', 'twitch:sora_slate', 'Sora Slate', 'https://twitch.tv/sora_slate', TIMESTAMPTZ '2026-06-27 12:10:00+00');

            WITH active_names(name, slug) AS (
                VALUES
                    ('Aki Lumina', 'aki_lumina'), ('Miki Mint', 'miki_mint'), ('Nova Nym', 'nova_nym'),
                    ('Mira Orbit', 'mira_orbit'), ('Taro Tactics', 'taro_tactics'), ('Yuna Quest', 'yuna_quest'),
                    ('Nova Nym Prime', 'nova_nym_prime'), ('Rin Replay', 'rin_replay'), ('Kira Comet', 'kira_comet'),
                    ('Melo Moon', 'melo_moon'), ('Echo Rill', 'echo_rill'), ('Vivi Verse', 'vivi_verse'),
                    ('Luna Koi', 'luna_koi'), ('Hana Hertz', 'hana_hertz'), ('Nyra Novel', 'nyra_novel'),
                    ('Vera Volt', 'vera_volt'), ('Kai Myth', 'kai_myth'), ('Mina Maze', 'mina_maze'),
                    ('Iris Ink', 'iris_ink'), ('Pia Palette', 'pia_palette'), ('Theo Thimble', 'theo_thimble'),
                    ('Neon Nori', 'neon_nori'), ('Slate Sen', 'slate_sen'), ('Momo Motion', 'momo_motion'),
                    ('Chroma Vale', 'chroma_vale'), ('Riku Render', 'riku_render'), ('Faye Flux', 'faye_flux'),
                    ('Runa Bits', 'runa_bits'), ('Bibi Beacon', 'bibi_beacon'), ('Ori Opal', 'ori_opal'),
                    ('Sora Slate', 'sora_slate'), ('Cleo Campfire', 'cleo_campfire'), ('Juno Jamboree', 'juno_jamboree'),
                    ('Ember Vail', 'ember_vail'), ('Riku Relay', 'riku_relay'), ('Nami Node', 'nami_node')
            ),
            numbered_active AS (
                SELECT row_number() OVER () AS rn, name, slug FROM active_names
            )
            INSERT INTO "Candidates" (
                "Id", "SeasonId", "CategoryId", "StreamerIdentityId", "DisplayName", "ChannelSlug", "Platform",
                "NominationTally", "AcceptanceStatus", "AcceptanceNote", "ClipCompilationUrl", "ClipCompilationTitle", "ClipCompilationPlatform", "ClipEmbedStatus"
            )
            SELECT 2000 + rn, 1001, 1101 + ((rn - 1) / 3)::int,
                   CASE WHEN rn <= 6 THEN (3000 + rn)::integer ELSE NULL::integer END,
                   name, slug, 'Twitch',
                   3 + ((rn * 2) % 11),
                   CASE WHEN rn % 6 = 0 THEN 'pending' ELSE 'approved' END,
                   CASE WHEN rn IN (1, 10, 16, 25, 34) THEN 'Demo-Highlight fuer Review gepflegt.' ELSE NULL END,
                   CASE rn
                       WHEN 1 THEN 'https://youtu.be/demo-aki-rise'
                       WHEN 7 THEN 'https://clips.twitch.tv/demo-nova-finale'
                       WHEN 10 THEN 'https://youtu.be/demo-melo-live'
                       WHEN 16 THEN 'https://youtu.be/demo-vera-stage'
                       WHEN 25 THEN 'https://youtu.be/demo-chroma-design'
                       WHEN 34 THEN 'https://clips.twitch.tv/demo-ember-moment'
                       ELSE NULL
                   END,
                   CASE rn
                       WHEN 1 THEN 'Aki Lumina Hidden Star Reel'
                       WHEN 7 THEN 'Nova Nym Shining Clip'
                       WHEN 10 THEN 'Melo Moon Live Set'
                       WHEN 16 THEN 'Vera Volt Stage Reel'
                       WHEN 25 THEN 'Chroma Vale Design Reel'
                       WHEN 34 THEN 'Ember Vail Finale Clip'
                       ELSE NULL
                   END,
                   CASE WHEN rn IN (7, 34) THEN 'Twitch' WHEN rn IN (1, 10, 16, 25) THEN 'YouTube' ELSE NULL END,
                   CASE WHEN rn IN (1, 7, 10, 16, 25, 34) THEN 'available' ELSE 'unchecked' END
            FROM numbered_active;

            WITH archive_2025_names(name, slug) AS (
                VALUES
                    ('Archiv Aster', 'archiv_aster'), ('Archiv Beryl', 'archiv_beryl'), ('Archiv Coda', 'archiv_coda'),
                    ('Archiv Drift', 'archiv_drift'), ('Archiv Elara', 'archiv_elara'), ('Archiv Finch', 'archiv_finch'),
                    ('Archiv Lyra', 'archiv_lyra'), ('Archiv Muse', 'archiv_muse'), ('Archiv Nia', 'archiv_nia')
            ),
            archive_2024_names(name, slug) AS (
                VALUES
                    ('Archiv Poppy', 'archiv_poppy'), ('Archiv Quartz', 'archiv_quartz'), ('Archiv Rune', 'archiv_rune'),
                    ('Archiv Sol', 'archiv_sol'), ('Archiv Tide', 'archiv_tide'), ('Archiv Uma', 'archiv_uma')
            ),
            numbered_2025 AS (
                SELECT row_number() OVER () AS rn, name, slug FROM archive_2025_names
            ),
            numbered_2024 AS (
                SELECT row_number() OVER () AS rn, name, slug FROM archive_2024_names
            )
            INSERT INTO "Candidates" (
                "Id", "SeasonId", "CategoryId", "StreamerIdentityId", "DisplayName", "ChannelSlug", "Platform",
                "NominationTally", "AcceptanceStatus", "AcceptanceNote", "ClipCompilationUrl", "ClipCompilationTitle", "ClipCompilationPlatform", "ClipEmbedStatus"
            )
            SELECT 2100 + rn, 1000, 1200 + rn, NULL::integer, name, slug, 'Twitch', 7 + rn, 'approved', NULL,
                   CASE WHEN rn IN (3, 6, 9) THEN 'https://clips.twitch.tv/demo-' || slug ELSE 'https://youtu.be/demo-' || slug END,
                   name || ' Winner Reel',
                   CASE WHEN rn IN (3, 6, 9) THEN 'Twitch' ELSE 'YouTube' END,
                   'available'
            FROM numbered_2025
            UNION ALL
            SELECT 2200 + rn, 999, 1300 + rn, NULL::integer, name, slug, 'Twitch', 6 + rn, 'approved', NULL,
                   CASE WHEN rn IN (3, 6) THEN 'https://clips.twitch.tv/demo-' || slug ELSE 'https://youtu.be/demo-' || slug END,
                   name || ' Winner Reel',
                   CASE WHEN rn IN (3, 6) THEN 'Twitch' ELSE 'YouTube' END,
                   'available'
            FROM numbered_2024;

            INSERT INTO "Nominations" (
                "Id", "SeasonId", "CategoryId", "CategoryGroupName", "SubmittedByTwitchId", "CandidateId", "StreamerIdentityId", "SuggestedCategoryId",
                "CandidateText", "StreamUrl", "ResolvedChannel", "ResolvedPlatform", "AvgViewers", "HoursStreamed", "HoursWatched", "PeakViewers", "FollowersGained",
                "TrackerStatus", "TrackerCheckedAt", "TrackingReviewStatus", "TrackingFlagsJson", "TrackingReviewNote", "TrackingReviewedByTwitchId", "TrackingReviewedAt",
                "Status", "ReviewNote", "ReviewedByTwitchId", "CreatedAt", "ReviewedAt"
            )
            VALUES
                (4001, 1001, 1101, 'Gaming', 'viewer_1001', 2001, 3001, NULL, 'Aki Lumina', 'https://twitch.tv/aki_lumina', 'aki_lumina', 'Twitch', 14, 42, 2600, 48, 210, 'resolved', TIMESTAMPTZ '2026-06-18 09:20:00+00', 'clear', '[]', NULL, 'reviewer_demo', TIMESTAMPTZ '2026-06-18 10:10:00+00', 'approved', 'Hidden tier passt.', 'reviewer_demo', TIMESTAMPTZ '2026-06-02 18:30:00+00', TIMESTAMPTZ '2026-06-18 10:10:00+00'),
                (4002, 1001, 1102, 'Gaming', 'viewer_1002', 2004, 3002, NULL, 'Mira Orbit', 'https://twitch.tv/mira_orbit', 'mira_orbit', 'Twitch', 38, 58, 5900, 96, 510, 'resolved', TIMESTAMPTZ '2026-06-18 09:24:00+00', 'clear', '[]', NULL, 'reviewer_demo', TIMESTAMPTZ '2026-06-18 10:15:00+00', 'approved', NULL, 'reviewer_demo', TIMESTAMPTZ '2026-06-03 20:15:00+00', TIMESTAMPTZ '2026-06-18 10:15:00+00'),
                (4003, 1001, 1103, 'Gaming', 'viewer_1003', 2007, 3003, NULL, 'Nova Nym Prime', 'https://twitch.tv/nova_nym_prime', 'nova_nym_prime', 'Twitch', 116, 44, 9100, 420, 1200, 'resolved', TIMESTAMPTZ '2026-06-18 09:30:00+00', 'clear', '[]', NULL, 'reviewer_demo', TIMESTAMPTZ '2026-06-18 10:22:00+00', 'approved', 'Shining tier passt.', 'reviewer_demo', TIMESTAMPTZ '2026-06-04 14:45:00+00', TIMESTAMPTZ '2026-06-18 10:22:00+00'),
                (4004, 1001, 1104, 'Music', 'viewer_1004', 2010, NULL, NULL, 'Melo Moon', 'https://twitch.tv/melo_moon', 'melo_moon', 'Twitch', 18, 35, 2800, 52, 240, 'resolved', TIMESTAMPTZ '2026-06-18 09:40:00+00', 'clear', '[]', NULL, 'reviewer_demo', TIMESTAMPTZ '2026-06-18 10:30:00+00', 'approved', NULL, 'reviewer_demo', TIMESTAMPTZ '2026-06-05 21:05:00+00', TIMESTAMPTZ '2026-06-18 10:30:00+00'),
                (4005, 1001, 1105, 'Music', 'viewer_1005', 2013, 3004, NULL, 'Luna Koi', 'https://twitch.tv/luna_koi', 'luna_koi', 'Twitch', 45, 51, 6100, 110, 620, 'resolved', TIMESTAMPTZ '2026-06-18 09:45:00+00', 'clear', '[]', NULL, 'reviewer_demo', TIMESTAMPTZ '2026-06-18 10:36:00+00', 'approved', NULL, 'reviewer_demo', TIMESTAMPTZ '2026-06-06 19:25:00+00', TIMESTAMPTZ '2026-06-18 10:36:00+00'),
                (4006, 1001, 1106, 'Music', 'viewer_1006', 2016, NULL, NULL, 'Vera Volt', 'https://twitch.tv/vera_volt', 'vera_volt', 'Twitch', 88, 35, 7400, 210, 430, 'resolved', TIMESTAMPTZ '2026-06-18 09:48:00+00', 'clear', '[]', NULL, 'reviewer_demo', TIMESTAMPTZ '2026-06-18 10:41:00+00', 'approved', 'Live-Musik-Set pruefbar.', 'reviewer_demo', TIMESTAMPTZ '2026-06-07 17:55:00+00', TIMESTAMPTZ '2026-06-18 10:41:00+00'),
                (4007, 1001, 1107, 'Art & Design', 'viewer_1007', 2019, NULL, NULL, 'Iris Ink', 'https://twitch.tv/iris_ink', 'iris_ink', 'Twitch', 16, 31, 2100, 41, 180, 'resolved', TIMESTAMPTZ '2026-06-18 09:52:00+00', 'clear', '[]', NULL, 'reviewer_demo', TIMESTAMPTZ '2026-06-18 11:00:00+00', 'approved', NULL, 'reviewer_demo', TIMESTAMPTZ '2026-06-08 18:10:00+00', TIMESTAMPTZ '2026-06-18 11:00:00+00'),
                (4008, 1001, 1108, 'Art & Design', 'viewer_1008', 2022, NULL, NULL, 'Neon Nori', 'https://twitch.tv/neon_nori', 'neon_nori', 'Twitch', 52, 65, 31800, 130, 920, 'resolved', TIMESTAMPTZ '2026-06-18 09:58:00+00', 'clear', '[]', NULL, 'reviewer_demo', TIMESTAMPTZ '2026-06-18 11:06:00+00', 'approved', NULL, 'reviewer_demo', TIMESTAMPTZ '2026-06-09 16:20:00+00', TIMESTAMPTZ '2026-06-18 11:06:00+00'),
                (4009, 1001, 1109, 'Art & Design', 'viewer_1009', 2025, NULL, NULL, 'Chroma Vale', 'https://twitch.tv/chroma_vale', 'chroma_vale', 'Twitch', 177, 54, 24200, 440, 610, 'resolved', TIMESTAMPTZ '2026-06-18 10:02:00+00', 'clear', '[]', NULL, 'reviewer_demo', TIMESTAMPTZ '2026-06-18 11:12:00+00', 'approved', NULL, 'reviewer_demo', TIMESTAMPTZ '2026-06-10 20:40:00+00', TIMESTAMPTZ '2026-06-18 11:12:00+00'),
                (4010, 1001, 1110, 'Community', 'viewer_1010', 2028, 3005, NULL, 'Runa Bits', 'https://twitch.tv/runa_bits', 'runa_bits', 'Twitch', 12, 49, 1700, 29, 120, 'resolved', TIMESTAMPTZ '2026-06-18 10:08:00+00', 'clear', '[]', NULL, 'reviewer_demo', TIMESTAMPTZ '2026-06-18 11:18:00+00', 'approved', 'Community-Event mit Planungsdoku.', 'reviewer_demo', TIMESTAMPTZ '2026-06-11 15:50:00+00', TIMESTAMPTZ '2026-06-18 11:18:00+00'),
                (4011, 1001, 1111, 'Community', 'viewer_1011', 2031, 3006, NULL, 'Sora Slate', 'https://twitch.tv/sora_slate', 'sora_slate', 'Twitch', 44, 37, 11800, 92, 340, 'resolved', TIMESTAMPTZ '2026-06-18 10:14:00+00', 'clear', '[]', NULL, 'reviewer_demo', TIMESTAMPTZ '2026-06-18 11:24:00+00', 'approved', NULL, 'reviewer_demo', TIMESTAMPTZ '2026-06-12 19:05:00+00', TIMESTAMPTZ '2026-06-18 11:24:00+00'),
                (4012, 1001, 1112, 'Community', 'viewer_1012', 2034, NULL, NULL, 'Ember Vail', 'https://twitch.tv/ember_vail', 'ember_vail', 'Twitch', NULL, NULL, NULL, NULL, NULL, 'no_data', TIMESTAMPTZ '2026-06-18 10:18:00+00', 'needs_review', '[{"key":"no_tracker_data","label":"Keine Tracker-Daten","severity":"medium","description":"TwitchTracker hat keinen belastbaren Summary-Wert geliefert.","requiresManualReview":true,"blocksApproval":false,"adminNoteRequiredOnOverride":false}]', 'Manueller Review noetig.', NULL, NULL, 'pending', 'Tracker-Daten nachfordern.', NULL, TIMESTAMPTZ '2026-06-13 22:15:00+00', NULL);

            INSERT INTO "VoteBallots" ("Id", "SeasonId", "SubmittedByTwitchId", "Status", "SubmittedAt")
            VALUES
                (5001, 1001, 'vote_user_001', 'submitted', TIMESTAMPTZ '2026-06-25 18:10:00+00'),
                (5002, 1001, 'vote_user_002', 'submitted', TIMESTAMPTZ '2026-06-25 18:16:00+00'),
                (5003, 1001, 'vote_user_003', 'submitted', TIMESTAMPTZ '2026-06-25 19:05:00+00'),
                (5004, 1001, 'vote_user_004', 'submitted', TIMESTAMPTZ '2026-06-26 12:45:00+00'),
                (5005, 1001, 'vote_user_005', 'submitted', TIMESTAMPTZ '2026-06-26 20:30:00+00'),
                (5006, 1001, 'vote_user_006', 'submitted', TIMESTAMPTZ '2026-06-27 09:20:00+00'),
                (5007, 1001, 'vote_user_007', 'draft', TIMESTAMPTZ '2026-06-27 14:15:00+00'),
                (5008, 1001, 'vote_user_008', 'submitted', TIMESTAMPTZ '2026-06-28 21:05:00+00');

            INSERT INTO "VoteEntries" ("Id", "BallotId", "CategoryId", "CandidateId")
            VALUES
                (5101, 5001, 1101, 2001), (5102, 5001, 1104, 2010), (5103, 5001, 1107, 2019), (5104, 5001, 1110, 2028),
                (5105, 5002, 1102, 2004), (5106, 5002, 1105, 2013), (5107, 5002, 1108, 2022), (5108, 5002, 1111, 2031),
                (5109, 5003, 1103, 2007), (5110, 5003, 1106, 2016), (5111, 5003, 1109, 2025), (5112, 5003, 1112, 2034),
                (5113, 5004, 1101, 2002), (5114, 5004, 1104, 2011), (5115, 5004, 1107, 2020), (5116, 5004, 1110, 2029),
                (5117, 5005, 1102, 2005), (5118, 5005, 1105, 2014), (5119, 5005, 1108, 2023), (5120, 5005, 1111, 2032),
                (5121, 5006, 1103, 2008), (5122, 5006, 1106, 2017), (5123, 5006, 1109, 2026), (5124, 5006, 1112, 2035),
                (5125, 5007, 1101, 2003), (5126, 5007, 1104, 2012),
                (5127, 5008, 1102, 2004), (5128, 5008, 1105, 2013), (5129, 5008, 1108, 2022), (5130, 5008, 1111, 2031);

            INSERT INTO "Results" ("Id", "SeasonId", "CategoryId", "CandidateId", "CategoryName")
            SELECT 6000 + "Id" - 1200, 1000, "Id", 2100 + "Id" - 1200, "Name" FROM "Categories" WHERE "SeasonId" = 1000
            UNION ALL
            SELECT 6100 + "Id" - 1300, 999, "Id", 2200 + "Id" - 1300, "Name" FROM "Categories" WHERE "SeasonId" = 999;

            INSERT INTO "ClipSubmissions" (
                "Id", "SeasonId", "CategoryId", "CandidateId", "SubmittedByTwitchId", "ClipUrl", "Title", "Creator", "Platform",
                "Status", "ReviewNote", "ReviewedByTwitchId", "CreatedFromIp", "CreatedAt", "ReviewedAt"
            )
            VALUES
                (7001, 1001, 1103, 2007, 'viewer_2001', 'https://clips.twitch.tv/demo-nova-finale', 'Nova turns the boss fight', 'viewer_2001', 'Twitch', 'approved', 'Kontext passt.', 'clip_reviewer', '127.0.0.1', TIMESTAMPTZ '2026-06-20 18:12:00+00', TIMESTAMPTZ '2026-06-21 09:00:00+00'),
                (7002, 1001, 1106, 2016, 'viewer_2002', 'https://youtu.be/demo-vera-stage', 'Vera sings the finale bridge', 'viewer_2002', 'YouTube', 'approved', NULL, 'clip_reviewer', '127.0.0.1', TIMESTAMPTZ '2026-06-20 20:25:00+00', TIMESTAMPTZ '2026-06-21 09:08:00+00'),
                (7003, 1001, 1112, 2034, 'viewer_2003', 'https://clips.twitch.tv/demo-ember-moment', 'Ember opens the community event', 'viewer_2003', 'Twitch', 'pending', 'Timing pruefen.', NULL, '127.0.0.1', TIMESTAMPTZ '2026-06-21 11:40:00+00', NULL),
                (7004, 1001, 1109, 2025, 'viewer_2004', 'https://youtu.be/demo-chroma-design', 'Chroma explains the overlay rebuild', 'viewer_2004', 'YouTube', 'approved', NULL, 'clip_reviewer', '127.0.0.1', TIMESTAMPTZ '2026-06-21 15:10:00+00', TIMESTAMPTZ '2026-06-22 08:45:00+00'),
                (7005, 1001, 1111, 2031, 'viewer_2005', 'https://clips.twitch.tv/demo-sora-community', 'Sora lets chat design the scene', 'viewer_2005', 'Twitch', 'pending', NULL, NULL, '127.0.0.1', TIMESTAMPTZ '2026-06-22 19:55:00+00', NULL),
                (7006, 1000, 1203, 2103, 'archiv_viewer_1', 'https://clips.twitch.tv/demo-archiv-coda', 'Archiv Coda finale clip', 'archiv_viewer_1', 'Twitch', 'approved', NULL, 'clip_reviewer', '127.0.0.1', TIMESTAMPTZ '2025-06-10 19:00:00+00', TIMESTAMPTZ '2025-06-11 09:00:00+00'),
                (7007, 999, 1303, 2203, 'archiv_viewer_2', 'https://clips.twitch.tv/demo-archiv-rune', 'Archiv Rune classic moment', 'archiv_viewer_2', 'Twitch', 'approved', NULL, 'clip_reviewer', '127.0.0.1', TIMESTAMPTZ '2024-06-10 19:00:00+00', TIMESTAMPTZ '2024-06-11 09:00:00+00');

            INSERT INTO "ShowactApplications" (
                "Id", "SeasonId", "ArtistName", "ContactEmail", "ContactDiscord", "PlatformUrl", "PerformanceType", "Description",
                "TechnicalNotes", "ReferenceUrl", "FieldResponsesJson", "Status", "ReviewNote", "ReviewedByTwitchId", "CreatedFromIp", "UserAgent", "CreatedAt", "ReviewedAt"
            )
            VALUES
                (8001, 1001, 'Melo Moon', 'melo@example.invalid', 'melo_moon', 'https://twitch.tv/melo_moon', 'Live-Gesang', 'Akustisches Opening-Medley mit zwei kurzen Songs.', 'Benoetigt Instrumentalspur und Monitoring.', 'https://youtu.be/demo-melo-live', '{"performanceLength":"6 Minuten","contentRating":"family friendly"}', 'approved', 'Passt als Opener.', 'show_reviewer', '127.0.0.1', 'DemoBrowser/1.0', TIMESTAMPTZ '2026-06-19 13:10:00+00', TIMESTAMPTZ '2026-06-20 10:00:00+00'),
                (8002, 1001, 'Neon Nori', 'nori@example.invalid', 'neon_nori', 'https://twitch.tv/neon_nori', 'Visual Interlude', 'Kurze Motion-Overlay-Performance zwischen zwei Award-Bloecken.', 'OBS-Szene mit WebM-Loop, kein Mikro.', 'https://youtu.be/demo-nori-motion', '{"performanceLength":"3 Minuten","contentRating":"keine Hinweise"}', 'pending', NULL, NULL, '127.0.0.1', 'DemoBrowser/1.0', TIMESTAMPTZ '2026-06-20 16:40:00+00', NULL),
                (8003, 1000, 'Archiv Lyra', 'lyra@example.invalid', 'archiv_lyra', 'https://twitch.tv/archiv_lyra', 'Archiv Musik', 'Gewinner-Showcase aus dem Vorjahr.', 'VOD vorhanden.', 'https://youtu.be/demo-archiv-lyra', '{"performanceLength":"4 Minuten","contentRating":"family friendly"}', 'approved', 'Archiv-Showact.', 'show_reviewer', '127.0.0.1', 'DemoBrowser/1.0', TIMESTAMPTZ '2025-06-01 12:05:00+00', TIMESTAMPTZ '2025-06-02 09:30:00+00'),
                (8004, 999, 'Archiv Sol', 'sol@example.invalid', 'archiv_sol', 'https://twitch.tv/archiv_sol', 'Archiv Musik', 'Aelteres Archiv-Showcase fuer den Archiv-Endpunkt.', 'VOD vorhanden.', 'https://youtu.be/demo-archiv-sol', '{"performanceLength":"4 Minuten","contentRating":"family friendly"}', 'approved', 'Archiv-Showact.', 'show_reviewer', '127.0.0.1', 'DemoBrowser/1.0', TIMESTAMPTZ '2024-06-01 12:05:00+00', TIMESTAMPTZ '2024-06-02 09:30:00+00');

            INSERT INTO "Sponsors" (
                "Id", "SeasonId", "Name", "WebsiteUrl", "LogoUrl", "Description", "Tier", "SortOrder", "IsVisible", "CreatedAt", "UpdatedAt"
            )
            VALUES
                (9001, 1001, 'CloudBeacon Hosting', 'https://example.invalid/cloudbeacon', '/demo/sponsors/cloudbeacon-hosting.svg', 'Server- und Bot-Hosting fuer Community-Projekte.', 'Main Partner', 10, TRUE, TIMESTAMPTZ '2026-06-01 10:00:00+00', NULL),
                (9002, 1001, 'NekoPixel Energy', 'https://example.invalid/nekopixel', '/demo/sponsors/nekopixel-energy.svg', 'Fiktiver Energy-Drink fuer lange Stream-Naechte.', 'Gold Partner', 20, TRUE, TIMESTAMPTZ '2026-06-01 10:05:00+00', NULL),
                (9003, 1001, 'PrismLoop Audio', 'https://example.invalid/prismloop', '/demo/sponsors/prismloop-audio.svg', 'Audio-Tools und Soundpacks fuer Creator:innen.', 'Gold Partner', 30, TRUE, TIMESTAMPTZ '2026-06-01 10:10:00+00', NULL),
                (9004, 1001, 'HoshiForge Studio', 'https://example.invalid/hoshiforge', '/demo/sponsors/hoshiforge-studio.svg', 'Branding, Overlays und kleine Motion-Pakete.', 'Community Partner', 40, TRUE, TIMESTAMPTZ '2026-06-01 10:15:00+00', NULL),
                (9005, 1001, 'ChibiCanvas Market', 'https://example.invalid/chibicanvas', '/demo/sponsors/chibicanvas-market.svg', 'Asset-Marktplatz fuer Panels, Emotes und Stream-Grafiken.', 'Community Partner', 50, TRUE, TIMESTAMPTZ '2026-06-01 10:20:00+00', NULL);

            INSERT INTO "RiskFlags" (
                "Id", "SeasonId", "TwitchUserId", "Source", "Type", "Severity", "Status", "Summary", "CreatedFromIp", "UserAgent",
                "MetadataJson", "ReviewNote", "ReviewedByTwitchId", "CreatedAt", "ReviewedAt"
            )
            VALUES
                (10001, 1001, 'viewer_1012', 'tracking-review', 'no_tracker_data', 'medium', 'open', 'TwitchTracker lieferte keine belastbaren Daten fuer Ember Vail.', '127.0.0.1', 'DemoBrowser/1.0', '{"seasonId":1001,"nominationId":4012}', 'Manuelle Community-Pruefung vor Finale.', 'reviewer_demo', TIMESTAMPTZ '2026-06-18 11:02:00+00', TIMESTAMPTZ '2026-06-18 11:08:00+00'),
                (10002, 1001, 'vote_user_007', 'voting', 'draft_ballot', 'low', 'resolved', 'Draft-Ballot ohne Submission, sichtbar fuer Dashboard-Randfall.', '127.0.0.1', 'DemoBrowser/1.0', '{"seasonId":1001,"ballotId":5007}', 'Nur Demo-Randfall.', 'reviewer_demo', TIMESTAMPTZ '2026-06-27 14:20:00+00', TIMESTAMPTZ '2026-06-27 15:00:00+00'),
                (10003, 1000, 'archiv_viewer_1', 'archive-cleanup', 'archived_clip', 'low', 'resolved', 'Archiv-Clip 2025 wurde geprueft.', '127.0.0.1', 'DemoBrowser/1.0', '{"seasonId":1000,"clipId":7006}', 'Archiv okay.', 'reviewer_demo', TIMESTAMPTZ '2025-06-11 09:10:00+00', TIMESTAMPTZ '2025-06-11 09:25:00+00'),
                (10004, 999, 'archiv_viewer_2', 'archive-cleanup', 'archived_clip', 'low', 'resolved', 'Archiv-Clip 2024 wurde geprueft.', '127.0.0.1', 'DemoBrowser/1.0', '{"seasonId":999,"clipId":7007}', 'Archiv okay.', 'reviewer_demo', TIMESTAMPTZ '2024-06-11 09:10:00+00', TIMESTAMPTZ '2024-06-11 09:25:00+00');

            SELECT setval(pg_get_serial_sequence('"Seasons"', 'Id'), COALESCE((SELECT MAX("Id") FROM "Seasons"), 1));
            SELECT setval(pg_get_serial_sequence('"Categories"', 'Id'), COALESCE((SELECT MAX("Id") FROM "Categories"), 1));
            SELECT setval(pg_get_serial_sequence('"StreamerIdentities"', 'Id'), COALESCE((SELECT MAX("Id") FROM "StreamerIdentities"), 1));
            SELECT setval(pg_get_serial_sequence('"Candidates"', 'Id'), COALESCE((SELECT MAX("Id") FROM "Candidates"), 1));
            SELECT setval(pg_get_serial_sequence('"Nominations"', 'Id'), COALESCE((SELECT MAX("Id") FROM "Nominations"), 1));
            SELECT setval(pg_get_serial_sequence('"VoteBallots"', 'Id'), COALESCE((SELECT MAX("Id") FROM "VoteBallots"), 1));
            SELECT setval(pg_get_serial_sequence('"VoteEntries"', 'Id'), COALESCE((SELECT MAX("Id") FROM "VoteEntries"), 1));
            SELECT setval(pg_get_serial_sequence('"Results"', 'Id'), COALESCE((SELECT MAX("Id") FROM "Results"), 1));
            SELECT setval(pg_get_serial_sequence('"ClipSubmissions"', 'Id'), COALESCE((SELECT MAX("Id") FROM "ClipSubmissions"), 1));
            SELECT setval(pg_get_serial_sequence('"ShowactApplications"', 'Id'), COALESCE((SELECT MAX("Id") FROM "ShowactApplications"), 1));
            SELECT setval(pg_get_serial_sequence('"Sponsors"', 'Id'), COALESCE((SELECT MAX("Id") FROM "Sponsors"), 1));
            SELECT setval(pg_get_serial_sequence('"RiskFlags"', 'Id'), COALESCE((SELECT MAX("Id") FROM "RiskFlags"), 1));
            END;
            $vtsa_adjust$;
            """
        );
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.Sql(
            """
            DELETE FROM "RiskFlags" WHERE "SeasonId" IN (999, 1000, 1001);
            DELETE FROM "Seasons" WHERE "Id" IN (999, 1000, 1001) AND "IsDemo" = TRUE;
            DELETE FROM "StreamerIdentities" WHERE "Id" BETWEEN 3001 AND 3020;
            """
        );
    }
}
