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

CREATE TABLE "Seasons" (
    "Id" integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    "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
);

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

CREATE TABLE "Categories" (
    "Id" integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    "SeasonId" integer NOT NULL REFERENCES "Seasons" ("Id") ON DELETE CASCADE,
    "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
);

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

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

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

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

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

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

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 TABLE "Results" (
    "Id" integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    "SeasonId" integer NOT NULL REFERENCES "Seasons" ("Id") ON DELETE CASCADE,
    "CandidateId" integer NOT NULL REFERENCES "Candidates" ("Id") ON DELETE CASCADE,
    "CategoryName" character varying(120) NOT NULL
);

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

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

CREATE INDEX "IX_VoteEntries_BallotId" ON "VoteEntries" ("BallotId");
CREATE INDEX "IX_VoteEntries_CandidateId" ON "VoteEntries" ("CandidateId");
CREATE INDEX "IX_VoteEntries_CategoryId" ON "VoteEntries" ("CategoryId");

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', '2026-05-31', '2026-05-01', '2026-07-10', '2026-07-01', '2026-07-20', '2026-06-30', '2026-06-01', 2026),
(2, 'Archived', true, false, 'VTuber Star Awards 2025', '2025-05-31', '2025-05-01', '2025-07-10', '2025-07-01', '2025-07-20', '2025-06-30', '2025-06-01', 2025),
(3, 'Archived', true, false, 'VTuber Star Awards 2024', '2024-05-31', '2024-05-01', '2024-07-10', '2024-07-01', '2024-07-20', '2024-06-30', '2024-06-01', 2024),
(4, 'Archived', true, false, 'VTuber Star Awards 2023', '2023-05-31', '2023-05-01', '2023-07-10', '2023-07-01', '2023-07-20', '2023-06-30', '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),
(2, 'Events, Konzerte und 3D-Shows.', 'Performance', 3, 'Bestes Live Event', 1, 'bestes-live-event', 2),
(3, 'Der lustigste oder emotionalste Clip des Jahres.', 'Clips & Highlights', 3, 'Clip des Jahres', 1, 'clip-des-jahres', 3),
(4, 'Die aktivste und freundlichste Community.', 'Main Awards', 3, 'Beste Community', 1, 'beste-community', 4),
(5, 'Archivkategorie 2025.', 'Main Awards', 3, 'VTuber des Jahres', 2, 'vtuber-des-jahres', 1),
(6, 'Archivkategorie 2025.', 'Performance', 3, 'Bestes Live Event', 2, 'bestes-live-event', 2),
(7, 'Archivkategorie 2025.', 'Clips & Highlights', 3, 'Clip des Jahres', 2, 'clip-des-jahres', 3),
(8, 'Archivkategorie 2024.', 'Main Awards', 3, 'VTuber des Jahres', 3, 'vtuber-des-jahres', 1),
(9, 'Archivkategorie 2024.', 'Clips & Highlights', 3, 'Clip des Jahres', 3, 'clip-des-jahres', 2),
(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', '2026-06-11T12:00:00+00:00', 'twitch_vote_1'),
(2, 1, 'submitted', '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),
(2, 1, '@kurainu', 'Kurainu', 'Twitch', 1),
(3, 1, '@shiroch', 'Shiro Ch.', 'Twitch', 1),
(4, 2, '@kurainu', 'Kurainu 3D Live', 'Twitch', 1),
(5, 2, '@aoisakura', 'Aoi Sakura Showcase', 'YouTube', 1),
(6, 3, '@pyonkichikingdom', 'Pyonkichi Kingdom', 'Twitch', 1),
(7, 4, '@moonrelay', 'Moonrelay', 'Twitch', 1),
(8, 5, '@hoshimimiyu', 'Hoshimi Miyu', 'Twitch', 2),
(9, 6, '@kurainu', 'Kurainu 3D Live', 'Twitch', 2),
(10, 7, '@pyonkichikingdom', 'Pyonkichi Kingdom', 'Twitch', 2),
(11, 8, '@aoisakura', 'Aoi Sakura', 'YouTube', 3),
(12, 9, '@starbyte', 'Starbyte', 'Twitch', 3),
(13, 10, '@tenshivox', 'Tenshi Vox', 'Twitch', 4);

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

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

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

SELECT setval(pg_get_serial_sequence('"Seasons"', 'Id'), COALESCE(MAX("Id"), 1), true) FROM "Seasons";
SELECT setval(pg_get_serial_sequence('"Categories"', 'Id'), COALESCE(MAX("Id"), 1), true) FROM "Categories";
SELECT setval(pg_get_serial_sequence('"VoteBallots"', 'Id'), COALESCE(MAX("Id"), 1), true) FROM "VoteBallots";
SELECT setval(pg_get_serial_sequence('"Candidates"', 'Id'), COALESCE(MAX("Id"), 1), true) FROM "Candidates";
SELECT setval(pg_get_serial_sequence('"Nominations"', 'Id'), COALESCE(MAX("Id"), 1), true) FROM "Nominations";
SELECT setval(pg_get_serial_sequence('"Results"', 'Id'), COALESCE(MAX("Id"), 1), true) FROM "Results";
SELECT setval(pg_get_serial_sequence('"VoteEntries"', 'Id'), COALESCE(MAX("Id"), 1), true) FROM "VoteEntries";

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