﻿CREATE TABLE IF NOT EXISTS "__EFMigrationsHistory" (
    "MigrationId" character varying(150) NOT NULL,
    "ProductVersion" character varying(32) NOT NULL,
    CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY ("MigrationId")
);

START TRANSACTION;

CREATE TABLE "Seasons" (
    "Id" integer GENERATED BY DEFAULT AS IDENTITY,
    "Year" integer NOT NULL,
    "Name" character varying(160) NOT NULL,
    "IsCurrent" boolean NOT NULL,
    "IsCommunityOnly" boolean NOT NULL,
    "CurrentPhase" character varying(60) NOT NULL,
    "NominationStartsAt" date NOT NULL,
    "NominationEndsAt" date NOT NULL,
    "VotingStartsAt" date NOT NULL,
    "VotingEndsAt" date NOT NULL,
    "ReviewStartsAt" date NOT NULL,
    "ReviewEndsAt" date NOT NULL,
    "ShowDate" date NOT NULL,
    CONSTRAINT "PK_Seasons" PRIMARY KEY ("Id")
);

CREATE TABLE "Categories" (
    "Id" integer GENERATED BY DEFAULT AS IDENTITY,
    "SeasonId" integer NOT NULL,
    "GroupName" character varying(80) NOT NULL,
    "Name" character varying(120) NOT NULL,
    "Slug" text NOT NULL,
    "Description" character varying(400) NOT NULL,
    "SortOrder" integer NOT NULL,
    "MaxNomineesPerUser" integer NOT NULL,
    CONSTRAINT "PK_Categories" PRIMARY KEY ("Id"),
    CONSTRAINT "FK_Categories_Seasons_SeasonId" FOREIGN KEY ("SeasonId") REFERENCES "Seasons" ("Id") ON DELETE CASCADE
);

CREATE TABLE "VoteBallots" (
    "Id" integer GENERATED BY DEFAULT AS IDENTITY,
    "SeasonId" integer NOT NULL,
    "SubmittedByTwitchId" character varying(120) NOT NULL,
    "Status" character varying(30) NOT NULL,
    "SubmittedAt" timestamp with time zone NOT NULL,
    CONSTRAINT "PK_VoteBallots" PRIMARY KEY ("Id"),
    CONSTRAINT "FK_VoteBallots_Seasons_SeasonId" FOREIGN KEY ("SeasonId") REFERENCES "Seasons" ("Id") ON DELETE CASCADE
);

CREATE TABLE "Candidates" (
    "Id" integer GENERATED BY DEFAULT AS IDENTITY,
    "SeasonId" integer NOT NULL,
    "CategoryId" integer NOT NULL,
    "DisplayName" character varying(120) NOT NULL,
    "ChannelSlug" character varying(120) NOT NULL,
    "Platform" character varying(40) NOT NULL,
    CONSTRAINT "PK_Candidates" PRIMARY KEY ("Id"),
    CONSTRAINT "FK_Candidates_Categories_CategoryId" FOREIGN KEY ("CategoryId") REFERENCES "Categories" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_Candidates_Seasons_SeasonId" FOREIGN KEY ("SeasonId") REFERENCES "Seasons" ("Id") ON DELETE CASCADE
);

CREATE TABLE "Nominations" (
    "Id" integer GENERATED BY DEFAULT AS IDENTITY,
    "SeasonId" integer NOT NULL,
    "CategoryId" integer NOT NULL,
    "SubmittedByTwitchId" character varying(120) NOT NULL,
    "CandidateId" integer,
    "CandidateText" character varying(120),
    "CreatedAt" timestamp with time zone NOT NULL,
    CONSTRAINT "PK_Nominations" PRIMARY KEY ("Id"),
    CONSTRAINT "FK_Nominations_Candidates_CandidateId" FOREIGN KEY ("CandidateId") REFERENCES "Candidates" ("Id"),
    CONSTRAINT "FK_Nominations_Categories_CategoryId" FOREIGN KEY ("CategoryId") REFERENCES "Categories" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_Nominations_Seasons_SeasonId" FOREIGN KEY ("SeasonId") REFERENCES "Seasons" ("Id") ON DELETE CASCADE
);

CREATE TABLE "Results" (
    "Id" integer GENERATED BY DEFAULT AS IDENTITY,
    "SeasonId" integer NOT NULL,
    "CandidateId" integer NOT NULL,
    "CategoryName" character varying(120) NOT NULL,
    CONSTRAINT "PK_Results" PRIMARY KEY ("Id"),
    CONSTRAINT "FK_Results_Candidates_CandidateId" FOREIGN KEY ("CandidateId") REFERENCES "Candidates" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_Results_Seasons_SeasonId" FOREIGN KEY ("SeasonId") REFERENCES "Seasons" ("Id") ON DELETE CASCADE
);

CREATE TABLE "VoteEntries" (
    "Id" integer GENERATED BY DEFAULT AS IDENTITY,
    "BallotId" integer NOT NULL,
    "CategoryId" integer NOT NULL,
    "CandidateId" integer NOT NULL,
    CONSTRAINT "PK_VoteEntries" PRIMARY KEY ("Id"),
    CONSTRAINT "FK_VoteEntries_Candidates_CandidateId" FOREIGN KEY ("CandidateId") REFERENCES "Candidates" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_VoteEntries_Categories_CategoryId" FOREIGN KEY ("CategoryId") REFERENCES "Categories" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_VoteEntries_VoteBallots_BallotId" FOREIGN KEY ("BallotId") REFERENCES "VoteBallots" ("Id") ON DELETE CASCADE
);

INSERT INTO "Seasons" ("Id", "CurrentPhase", "IsCommunityOnly", "IsCurrent", "Name", "NominationEndsAt", "NominationStartsAt", "ReviewEndsAt", "ReviewStartsAt", "ShowDate", "VotingEndsAt", "VotingStartsAt", "Year")
VALUES (1, 'Community Voting', TRUE, TRUE, 'VTuber Star Awards 2026', DATE '2026-05-31', DATE '2026-05-01', DATE '2026-07-10', DATE '2026-07-01', DATE '2026-07-20', DATE '2026-06-30', DATE '2026-06-01', 2026);
INSERT INTO "Seasons" ("Id", "CurrentPhase", "IsCommunityOnly", "IsCurrent", "Name", "NominationEndsAt", "NominationStartsAt", "ReviewEndsAt", "ReviewStartsAt", "ShowDate", "VotingEndsAt", "VotingStartsAt", "Year")
VALUES (2, 'Archived', TRUE, FALSE, 'VTuber Star Awards 2025', DATE '2025-05-31', DATE '2025-05-01', DATE '2025-07-10', DATE '2025-07-01', DATE '2025-07-20', DATE '2025-06-30', DATE '2025-06-01', 2025);
INSERT INTO "Seasons" ("Id", "CurrentPhase", "IsCommunityOnly", "IsCurrent", "Name", "NominationEndsAt", "NominationStartsAt", "ReviewEndsAt", "ReviewStartsAt", "ShowDate", "VotingEndsAt", "VotingStartsAt", "Year")
VALUES (3, 'Archived', TRUE, FALSE, 'VTuber Star Awards 2024', DATE '2024-05-31', DATE '2024-05-01', DATE '2024-07-10', DATE '2024-07-01', DATE '2024-07-20', DATE '2024-06-30', DATE '2024-06-01', 2024);
INSERT INTO "Seasons" ("Id", "CurrentPhase", "IsCommunityOnly", "IsCurrent", "Name", "NominationEndsAt", "NominationStartsAt", "ReviewEndsAt", "ReviewStartsAt", "ShowDate", "VotingEndsAt", "VotingStartsAt", "Year")
VALUES (4, 'Archived', TRUE, FALSE, 'VTuber Star Awards 2023', DATE '2023-05-31', DATE '2023-05-01', DATE '2023-07-10', DATE '2023-07-01', DATE '2023-07-20', DATE '2023-06-30', DATE '2023-06-01', 2023);

INSERT INTO "Categories" ("Id", "Description", "GroupName", "MaxNomineesPerUser", "Name", "SeasonId", "Slug", "SortOrder")
VALUES (1, 'Die groesste Auszeichnung des Jahres.', 'Main Awards', 3, 'VTuber des Jahres', 1, 'vtuber-des-jahres', 1);
INSERT INTO "Categories" ("Id", "Description", "GroupName", "MaxNomineesPerUser", "Name", "SeasonId", "Slug", "SortOrder")
VALUES (2, 'Events, Konzerte und 3D-Shows.', 'Performance', 3, 'Bestes Live Event', 1, 'bestes-live-event', 2);
INSERT INTO "Categories" ("Id", "Description", "GroupName", "MaxNomineesPerUser", "Name", "SeasonId", "Slug", "SortOrder")
VALUES (3, 'Der lustigste oder emotionalste Clip des Jahres.', 'Clips & Highlights', 3, 'Clip des Jahres', 1, 'clip-des-jahres', 3);
INSERT INTO "Categories" ("Id", "Description", "GroupName", "MaxNomineesPerUser", "Name", "SeasonId", "Slug", "SortOrder")
VALUES (4, 'Die aktivste und freundlichste Community.', 'Main Awards', 3, 'Beste Community', 1, 'beste-community', 4);
INSERT INTO "Categories" ("Id", "Description", "GroupName", "MaxNomineesPerUser", "Name", "SeasonId", "Slug", "SortOrder")
VALUES (5, 'Archivkategorie 2025.', 'Main Awards', 3, 'VTuber des Jahres', 2, 'vtuber-des-jahres', 1);
INSERT INTO "Categories" ("Id", "Description", "GroupName", "MaxNomineesPerUser", "Name", "SeasonId", "Slug", "SortOrder")
VALUES (6, 'Archivkategorie 2025.', 'Performance', 3, 'Bestes Live Event', 2, 'bestes-live-event', 2);
INSERT INTO "Categories" ("Id", "Description", "GroupName", "MaxNomineesPerUser", "Name", "SeasonId", "Slug", "SortOrder")
VALUES (7, 'Archivkategorie 2025.', 'Clips & Highlights', 3, 'Clip des Jahres', 2, 'clip-des-jahres', 3);
INSERT INTO "Categories" ("Id", "Description", "GroupName", "MaxNomineesPerUser", "Name", "SeasonId", "Slug", "SortOrder")
VALUES (8, 'Archivkategorie 2024.', 'Main Awards', 3, 'VTuber des Jahres', 3, 'vtuber-des-jahres', 1);
INSERT INTO "Categories" ("Id", "Description", "GroupName", "MaxNomineesPerUser", "Name", "SeasonId", "Slug", "SortOrder")
VALUES (9, 'Archivkategorie 2024.', 'Clips & Highlights', 3, 'Clip des Jahres', 3, 'clip-des-jahres', 2);
INSERT INTO "Categories" ("Id", "Description", "GroupName", "MaxNomineesPerUser", "Name", "SeasonId", "Slug", "SortOrder")
VALUES (10, 'Archivkategorie 2023.', 'Main Awards', 3, 'VTuber des Jahres', 4, 'vtuber-des-jahres', 1);

INSERT INTO "VoteBallots" ("Id", "SeasonId", "Status", "SubmittedAt", "SubmittedByTwitchId")
VALUES (1, 1, 'submitted', TIMESTAMPTZ '2026-06-11T12:00:00+00:00', 'twitch_vote_1');
INSERT INTO "VoteBallots" ("Id", "SeasonId", "Status", "SubmittedAt", "SubmittedByTwitchId")
VALUES (2, 1, 'submitted', TIMESTAMPTZ '2026-06-11T12:05:00+00:00', 'twitch_vote_2');

INSERT INTO "Candidates" ("Id", "CategoryId", "ChannelSlug", "DisplayName", "Platform", "SeasonId")
VALUES (1, 1, '@hoshimimiyu', 'Hoshimi Miyu', 'Twitch', 1);
INSERT INTO "Candidates" ("Id", "CategoryId", "ChannelSlug", "DisplayName", "Platform", "SeasonId")
VALUES (2, 1, '@kurainu', 'Kurainu', 'Twitch', 1);
INSERT INTO "Candidates" ("Id", "CategoryId", "ChannelSlug", "DisplayName", "Platform", "SeasonId")
VALUES (3, 1, '@shiroch', 'Shiro Ch.', 'Twitch', 1);
INSERT INTO "Candidates" ("Id", "CategoryId", "ChannelSlug", "DisplayName", "Platform", "SeasonId")
VALUES (4, 2, '@kurainu', 'Kurainu 3D Live', 'Twitch', 1);
INSERT INTO "Candidates" ("Id", "CategoryId", "ChannelSlug", "DisplayName", "Platform", "SeasonId")
VALUES (5, 2, '@aoisakura', 'Aoi Sakura Showcase', 'YouTube', 1);
INSERT INTO "Candidates" ("Id", "CategoryId", "ChannelSlug", "DisplayName", "Platform", "SeasonId")
VALUES (6, 3, '@pyonkichikingdom', 'Pyonkichi Kingdom', 'Twitch', 1);
INSERT INTO "Candidates" ("Id", "CategoryId", "ChannelSlug", "DisplayName", "Platform", "SeasonId")
VALUES (7, 4, '@moonrelay', 'Moonrelay', 'Twitch', 1);
INSERT INTO "Candidates" ("Id", "CategoryId", "ChannelSlug", "DisplayName", "Platform", "SeasonId")
VALUES (8, 5, '@hoshimimiyu', 'Hoshimi Miyu', 'Twitch', 2);
INSERT INTO "Candidates" ("Id", "CategoryId", "ChannelSlug", "DisplayName", "Platform", "SeasonId")
VALUES (9, 6, '@kurainu', 'Kurainu 3D Live', 'Twitch', 2);
INSERT INTO "Candidates" ("Id", "CategoryId", "ChannelSlug", "DisplayName", "Platform", "SeasonId")
VALUES (10, 7, '@pyonkichikingdom', 'Pyonkichi Kingdom', 'Twitch', 2);
INSERT INTO "Candidates" ("Id", "CategoryId", "ChannelSlug", "DisplayName", "Platform", "SeasonId")
VALUES (11, 8, '@aoisakura', 'Aoi Sakura', 'YouTube', 3);
INSERT INTO "Candidates" ("Id", "CategoryId", "ChannelSlug", "DisplayName", "Platform", "SeasonId")
VALUES (12, 9, '@starbyte', 'Starbyte', 'Twitch', 3);
INSERT INTO "Candidates" ("Id", "CategoryId", "ChannelSlug", "DisplayName", "Platform", "SeasonId")
VALUES (13, 10, '@tenshivox', 'Tenshi Vox', 'Twitch', 4);

INSERT INTO "Nominations" ("Id", "CandidateId", "CandidateText", "CategoryId", "CreatedAt", "SeasonId", "SubmittedByTwitchId")
VALUES (1, NULL, 'Hoshimi Miyu', 1, TIMESTAMPTZ '2026-06-10T13:00:00+00:00', 1, 'twitch_hoshi');
INSERT INTO "Nominations" ("Id", "CandidateId", "CandidateText", "CategoryId", "CreatedAt", "SeasonId", "SubmittedByTwitchId")
VALUES (2, NULL, 'Kurainu 3D Live', 2, TIMESTAMPTZ '2026-06-10T14:00:00+00:00', 1, 'twitch_kurainu');

INSERT INTO "Results" ("Id", "CandidateId", "CategoryName", "SeasonId")
VALUES (1, 8, 'VTuber des Jahres', 2);
INSERT INTO "Results" ("Id", "CandidateId", "CategoryName", "SeasonId")
VALUES (2, 9, 'Bestes Live Event', 2);
INSERT INTO "Results" ("Id", "CandidateId", "CategoryName", "SeasonId")
VALUES (3, 10, 'Clip des Jahres', 2);
INSERT INTO "Results" ("Id", "CandidateId", "CategoryName", "SeasonId")
VALUES (4, 11, 'VTuber des Jahres', 3);
INSERT INTO "Results" ("Id", "CandidateId", "CategoryName", "SeasonId")
VALUES (5, 12, 'Clip des Jahres', 3);
INSERT INTO "Results" ("Id", "CandidateId", "CategoryName", "SeasonId")
VALUES (6, 13, 'VTuber des Jahres', 4);

INSERT INTO "VoteEntries" ("Id", "BallotId", "CandidateId", "CategoryId")
VALUES (1, 1, 1, 1);
INSERT INTO "VoteEntries" ("Id", "BallotId", "CandidateId", "CategoryId")
VALUES (2, 1, 4, 2);
INSERT INTO "VoteEntries" ("Id", "BallotId", "CandidateId", "CategoryId")
VALUES (3, 2, 2, 1);
INSERT INTO "VoteEntries" ("Id", "BallotId", "CandidateId", "CategoryId")
VALUES (4, 2, 6, 3);

CREATE INDEX "IX_Candidates_CategoryId" ON "Candidates" ("CategoryId");

CREATE INDEX "IX_Candidates_SeasonId" ON "Candidates" ("SeasonId");

CREATE UNIQUE INDEX "IX_Categories_SeasonId_Slug" ON "Categories" ("SeasonId", "Slug");

CREATE INDEX "IX_Nominations_CandidateId" ON "Nominations" ("CandidateId");

CREATE INDEX "IX_Nominations_CategoryId" ON "Nominations" ("CategoryId");

CREATE INDEX "IX_Nominations_SeasonId" ON "Nominations" ("SeasonId");

CREATE INDEX "IX_Results_CandidateId" ON "Results" ("CandidateId");

CREATE INDEX "IX_Results_SeasonId" ON "Results" ("SeasonId");

CREATE UNIQUE INDEX "IX_Seasons_Year" ON "Seasons" ("Year");

CREATE INDEX "IX_VoteBallots_SeasonId" ON "VoteBallots" ("SeasonId");

CREATE INDEX "IX_VoteEntries_BallotId" ON "VoteEntries" ("BallotId");

CREATE INDEX "IX_VoteEntries_CandidateId" ON "VoteEntries" ("CandidateId");

CREATE INDEX "IX_VoteEntries_CategoryId" ON "VoteEntries" ("CategoryId");

SELECT setval(
    pg_get_serial_sequence('"Seasons"', 'Id'),
    GREATEST(
        (SELECT MAX("Id") FROM "Seasons") + 1,
        nextval(pg_get_serial_sequence('"Seasons"', 'Id'))),
    false);
SELECT setval(
    pg_get_serial_sequence('"Categories"', 'Id'),
    GREATEST(
        (SELECT MAX("Id") FROM "Categories") + 1,
        nextval(pg_get_serial_sequence('"Categories"', 'Id'))),
    false);
SELECT setval(
    pg_get_serial_sequence('"VoteBallots"', 'Id'),
    GREATEST(
        (SELECT MAX("Id") FROM "VoteBallots") + 1,
        nextval(pg_get_serial_sequence('"VoteBallots"', 'Id'))),
    false);
SELECT setval(
    pg_get_serial_sequence('"Candidates"', 'Id'),
    GREATEST(
        (SELECT MAX("Id") FROM "Candidates") + 1,
        nextval(pg_get_serial_sequence('"Candidates"', 'Id'))),
    false);
SELECT setval(
    pg_get_serial_sequence('"Nominations"', 'Id'),
    GREATEST(
        (SELECT MAX("Id") FROM "Nominations") + 1,
        nextval(pg_get_serial_sequence('"Nominations"', 'Id'))),
    false);
SELECT setval(
    pg_get_serial_sequence('"Results"', 'Id'),
    GREATEST(
        (SELECT MAX("Id") FROM "Results") + 1,
        nextval(pg_get_serial_sequence('"Results"', 'Id'))),
    false);
SELECT setval(
    pg_get_serial_sequence('"VoteEntries"', 'Id'),
    GREATEST(
        (SELECT MAX("Id") FROM "VoteEntries") + 1,
        nextval(pg_get_serial_sequence('"VoteEntries"', 'Id'))),
    false);

INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES ('20260617060000_InitialCreate', '8.0.11');

COMMIT;

