using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace Backend.Migrations
{
    /// <inheritdoc />
    public partial class AddClipAdminMenuVisible : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql("""
                ALTER TABLE "SiteSettings"
                ADD COLUMN IF NOT EXISTS "ClipAdminMenuVisible" boolean NOT NULL DEFAULT FALSE;

                ALTER TABLE "SiteSettings"
                ADD COLUMN IF NOT EXISTS "ShowactApplicationDisabledMessage" character varying(240) NOT NULL DEFAULT '';

                ALTER TABLE "SiteSettings"
                ADD COLUMN IF NOT EXISTS "ShowactApplicationsEnabled" boolean NOT NULL DEFAULT FALSE;

                ALTER TABLE "SiteSettings"
                ADD COLUMN IF NOT EXISTS "ShowactsContent" text NOT NULL DEFAULT '';

                ALTER TABLE "SiteSettings"
                ADD COLUMN IF NOT EXISTS "ShowactsUrl" character varying(400) NOT NULL DEFAULT '';

                ALTER TABLE "SiteSettings"
                ADD COLUMN IF NOT EXISTS "SponsorsVisible" boolean NOT NULL DEFAULT TRUE;
                """);

            migrationBuilder.Sql("""
                CREATE TABLE IF NOT EXISTS "ShowactApplications" (
                    "Id" integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
                    "SeasonId" integer NOT NULL,
                    "ArtistName" character varying(120) NOT NULL,
                    "ContactEmail" character varying(180) NOT NULL,
                    "ContactDiscord" character varying(120) NOT NULL,
                    "PlatformUrl" character varying(500) NOT NULL,
                    "PerformanceType" character varying(80) NOT NULL,
                    "Description" character varying(1000) NOT NULL,
                    "TechnicalNotes" character varying(1000) NOT NULL,
                    "ReferenceUrl" character varying(500) NOT NULL,
                    "Status" character varying(20) NOT NULL,
                    "ReviewNote" character varying(500) NULL,
                    "ReviewedByTwitchId" character varying(120) NULL,
                    "CreatedFromIp" character varying(80) NOT NULL,
                    "UserAgent" character varying(400) NOT NULL,
                    "CreatedAt" timestamp with time zone NOT NULL,
                    "ReviewedAt" timestamp with time zone NULL
                );

                CREATE TABLE IF NOT EXISTS "Sponsors" (
                    "Id" integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
                    "SeasonId" integer NOT NULL,
                    "Name" character varying(120) NOT NULL,
                    "WebsiteUrl" character varying(500) NOT NULL,
                    "LogoUrl" character varying(500) NOT NULL,
                    "Description" character varying(500) NOT NULL,
                    "Tier" character varying(80) NOT NULL,
                    "SortOrder" integer NOT NULL,
                    "IsVisible" boolean NOT NULL,
                    "CreatedAt" timestamp with time zone NOT NULL,
                    "UpdatedAt" timestamp with time zone NULL
                );

                DO $$
                BEGIN
                    IF NOT EXISTS (
                        SELECT 1
                        FROM pg_constraint
                        WHERE conname = 'FK_ShowactApplications_Seasons_SeasonId'
                    ) THEN
                        ALTER TABLE "ShowactApplications"
                        ADD CONSTRAINT "FK_ShowactApplications_Seasons_SeasonId"
                        FOREIGN KEY ("SeasonId") REFERENCES "Seasons" ("Id")
                        ON DELETE CASCADE;
                    END IF;
                END $$;

                DO $$
                BEGIN
                    IF NOT EXISTS (
                        SELECT 1
                        FROM pg_constraint
                        WHERE conname = 'FK_Sponsors_Seasons_SeasonId'
                    ) THEN
                        ALTER TABLE "Sponsors"
                        ADD CONSTRAINT "FK_Sponsors_Seasons_SeasonId"
                        FOREIGN KEY ("SeasonId") REFERENCES "Seasons" ("Id")
                        ON DELETE CASCADE;
                    END IF;
                END $$;
                """);

            migrationBuilder.UpdateData(
                table: "SiteSettings",
                keyColumn: "Id",
                keyValue: 1,
                columns: new[] { "ClipAdminMenuVisible", "ShowactApplicationDisabledMessage", "ShowactsContent", "ShowactsUrl", "SponsorsVisible", "WorkflowRulesJson" },
                values: new object[] { true, "Showact-Bewerbungen sind aktuell geschlossen.", "Informationen zu Showact-Bewerbungen, Ablauf und Kontakt fuer die Award-Show.", "https://vtuber-star-awards.de/showacts", true, "[{\"key\":\"max_finalists_per_category\",\"label\":\"Finale Kandidat:innen pro Kategorie\",\"enabled\":true,\"limit\":4,\"mode\":\"block\",\"description\":\"Begrenzt, wie viele finale Kandidat:innen in einer Kategorie vorbereitet werden.\"},{\"key\":\"max_candidate_appearances\",\"label\":\"Kandidaturen pro Person\",\"enabled\":true,\"limit\":2,\"mode\":\"warn\",\"description\":\"Warnt oder blockiert, wenn dieselbe Person in zu vielen Kategorien final auftaucht.\"},{\"key\":\"max_winner_placements\",\"label\":\"Gewinnerpl\\u00E4tze pro Person\",\"enabled\":true,\"limit\":1,\"mode\":\"block\",\"description\":\"Verhindert oder warnt, wenn dieselbe Person mehrfach als Gewinner:in gesetzt wird.\"},{\"key\":\"winner_requires_clip\",\"label\":\"Gewinner braucht Clip-Link\",\"enabled\":true,\"limit\":1,\"mode\":\"block\",\"description\":\"Verhindert oder warnt, wenn ein Gewinner ohne gepflegten YouTube-/Twitch-Clip gesetzt wird.\"}]" });

            migrationBuilder.Sql("""
                CREATE INDEX IF NOT EXISTS "IX_ShowactApplications_SeasonId_Status"
                ON "ShowactApplications" ("SeasonId", "Status");

                CREATE INDEX IF NOT EXISTS "IX_Sponsors_SeasonId_IsVisible_SortOrder"
                ON "Sponsors" ("SeasonId", "IsVisible", "SortOrder");
                """);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql("""
                DROP TABLE IF EXISTS "ShowactApplications";

                DROP TABLE IF EXISTS "Sponsors";
                """);

            migrationBuilder.Sql("""
                ALTER TABLE "SiteSettings"
                DROP COLUMN IF EXISTS "ClipAdminMenuVisible";

                ALTER TABLE "SiteSettings"
                DROP COLUMN IF EXISTS "ShowactApplicationDisabledMessage";

                ALTER TABLE "SiteSettings"
                DROP COLUMN IF EXISTS "ShowactApplicationsEnabled";

                ALTER TABLE "SiteSettings"
                DROP COLUMN IF EXISTS "ShowactsContent";

                ALTER TABLE "SiteSettings"
                DROP COLUMN IF EXISTS "ShowactsUrl";

                ALTER TABLE "SiteSettings"
                DROP COLUMN IF EXISTS "SponsorsVisible";
                """);

            migrationBuilder.UpdateData(
                table: "SiteSettings",
                keyColumn: "Id",
                keyValue: 1,
                column: "WorkflowRulesJson",
                value: "[{\"key\":\"max_finalists_per_category\",\"label\":\"Finale Kandidat:innen pro Kategorie\",\"enabled\":true,\"limit\":4,\"mode\":\"block\",\"description\":\"Begrenzt, wie viele finale Kandidat:innen in einer Kategorie vorbereitet werden.\"},{\"key\":\"max_candidate_appearances\",\"label\":\"Kandidaturen pro Person\",\"enabled\":true,\"limit\":2,\"mode\":\"warn\",\"description\":\"Warnt oder blockiert, wenn dieselbe Person in zu vielen Kategorien final auftaucht.\"},{\"key\":\"max_winner_placements\",\"label\":\"Gewinnerpl\\u00E4tze pro Person\",\"enabled\":true,\"limit\":1,\"mode\":\"block\",\"description\":\"Verhindert oder warnt, wenn dieselbe Person mehrfach als Gewinner:in gesetzt wird.\"}]");
        }
    }
}
