using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace TeleWave.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class SharedJunctionsAndBumperLines : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // Перенос данных идёт до всех схемных операций: старые колонки ещё на месте.
            // Текст подблоков превращается в строки, «Сейчас/Далее» — в четыре строки с
            // плейсхолдерами, свободный текст — в две.
            migrationBuilder.Sql("""ALTER TABLE "BumperTextVariants" ADD COLUMN "Lines" jsonb;""");
            migrationBuilder.Sql(
                """
                UPDATE "BumperTextVariants" SET "Lines" = CASE WHEN "Kind" = 1 THEN
                    jsonb_build_array(
                        jsonb_build_object('Position', 0, 'Style', 0, 'Color', 0, 'Text', COALESCE("Line1", '')),
                        jsonb_build_object('Position', 1, 'Style', 1, 'Color', 1, 'Text', COALESCE("Line2", ''))
                    )
                ELSE
                    jsonb_build_array(
                        jsonb_build_object('Position', 0, 'Style', 0, 'Color', 0, 'Text', COALESCE("NowLabel", '')),
                        jsonb_build_object('Position', 1, 'Style', 1, 'Color', 1, 'Text', '{now.title}'),
                        jsonb_build_object('Position', 2, 'Style', 0, 'Color', 0, 'Text', COALESCE("NextLabel", '')),
                        jsonb_build_object('Position', 3, 'Style', 1, 'Color', 1, 'Text', '{next.title}')
                    )
                END;
                """
            );
            // Пустые строки выбрасываем и перенумеровываем — иначе в кадре останутся дыры.
            migrationBuilder.Sql(
                """
                UPDATE "BumperTextVariants" v SET "Lines" = f.lines
                FROM (
                    SELECT id, COALESCE(jsonb_agg(jsonb_set(elem, '{Position}', to_jsonb(rn - 1)) ORDER BY rn), '[]'::jsonb) AS lines
                    FROM (
                        SELECT t."Id" AS id, e.elem AS elem,
                               row_number() OVER (PARTITION BY t."Id" ORDER BY e.ord) AS rn
                        FROM "BumperTextVariants" t,
                             LATERAL jsonb_array_elements(t."Lines") WITH ORDINALITY AS e(elem, ord)
                        WHERE COALESCE(e.elem->>'Text', '') <> ''
                    ) x GROUP BY id
                ) f WHERE v."Id" = f.id;
                """
            );

            // Фон: «Сейчас/Далее» показывал постер следующего шоу, свободный текст — фон блока.
            migrationBuilder.Sql(
                """ALTER TABLE "BumperTextVariants" ADD COLUMN "Background" integer NOT NULL DEFAULT 0;"""
            );
            migrationBuilder.Sql(
                """UPDATE "BumperTextVariants" SET "Background" = CASE WHEN "Kind" = 1 THEN 0 ELSE 1 END;"""
            );

            // Шрифт переезжает с канала на блок — у блока он был канальным.
            migrationBuilder.Sql(
                """ALTER TABLE "BumperTemplate" ADD COLUMN "Font" integer NOT NULL DEFAULT 0;"""
            );
            migrationBuilder.Sql(
                """UPDATE "BumperTemplate" t SET "Font" = c."BumperFont" FROM "Channels" c WHERE c."Id" = t."ChannelId";"""
            );

            // Кэш заставок: сигнатура теперь считается по содержимому, а подставленного текста
            // у старых записей нет. Это кэш — он пересоберётся при ближайшей генерации.
            migrationBuilder.Sql("""DELETE FROM "BumperAssets";""");

            migrationBuilder.DropForeignKey(
                name: "FK_BumperTemplate_Channels_ChannelId",
                table: "BumperTemplate"
            );

            migrationBuilder.DropForeignKey(
                name: "FK_BumperTextVariants_BumperTemplate_BumperTemplateId",
                table: "BumperTextVariants"
            );

            migrationBuilder.DropIndex(
                name: "IX_JunctionTemplates_ChannelId",
                table: "JunctionTemplates"
            );

            migrationBuilder.DropIndex(
                name: "IX_BumperAssets_FromShowId_ToShowId_Signature",
                table: "BumperAssets"
            );

            migrationBuilder.DropPrimaryKey(name: "PK_BumperTemplate", table: "BumperTemplate");

            migrationBuilder.DropIndex(
                name: "IX_BumperTemplate_ChannelId_Position",
                table: "BumperTemplate"
            );

            migrationBuilder.DropColumn(name: "ChannelId", table: "JunctionTemplates");

            migrationBuilder.DropColumn(name: "BumperFont", table: "Channels");

            migrationBuilder.DropColumn(name: "BumperSelection", table: "Channels");

            migrationBuilder.DropColumn(name: "BumpersEnabled", table: "Channels");

            migrationBuilder.DropColumn(name: "Line1", table: "BumperTextVariants");

            migrationBuilder.DropColumn(name: "Line2", table: "BumperTextVariants");

            migrationBuilder.DropColumn(name: "NextLabel", table: "BumperTextVariants");

            migrationBuilder.DropColumn(name: "NowLabel", table: "BumperTextVariants");

            migrationBuilder.DropColumn(name: "ChannelId", table: "BumperAssets");

            migrationBuilder.DropColumn(name: "FromShowId", table: "BumperAssets");

            migrationBuilder.DropColumn(name: "ToShowId", table: "BumperAssets");

            migrationBuilder.DropColumn(name: "ChannelId", table: "BumperTemplate");

            migrationBuilder.RenameTable(name: "BumperTemplate", newName: "BumperTemplates");

            migrationBuilder.DropColumn(name: "Kind", table: "BumperTextVariants");

            migrationBuilder.DropColumn(name: "Position", table: "BumperTemplates");

            migrationBuilder.AddColumn<int>(
                name: "MaxTotalSeconds",
                table: "JunctionTemplates",
                type: "integer",
                nullable: true
            );

            migrationBuilder.AddColumn<Guid>(
                name: "BumperVariantId",
                table: "JunctionElements",
                type: "uuid",
                nullable: true
            );

            migrationBuilder.AddColumn<string>(
                name: "ChoiceKey",
                table: "JunctionElements",
                type: "character varying(64)",
                maxLength: 64,
                nullable: true
            );

            migrationBuilder.AddColumn<int>(
                name: "ChoiceWeight",
                table: "JunctionElements",
                type: "integer",
                nullable: false,
                defaultValue: 1
            );

            migrationBuilder.AddColumn<Guid>(
                name: "PosterShowId",
                table: "BumperAssets",
                type: "uuid",
                nullable: true
            );

            migrationBuilder.AddColumn<string>(
                name: "RenderedLinesJson",
                table: "BumperAssets",
                type: "jsonb",
                nullable: false,
                defaultValue: "[]"
            );

            migrationBuilder.AddPrimaryKey(
                name: "PK_BumperTemplates",
                table: "BumperTemplates",
                column: "Id"
            );

            migrationBuilder.CreateIndex(
                name: "IX_JunctionTemplates_Name",
                table: "JunctionTemplates",
                column: "Name"
            );

            migrationBuilder.CreateIndex(
                name: "IX_JunctionElements_BumperTemplateId",
                table: "JunctionElements",
                column: "BumperTemplateId"
            );

            migrationBuilder.CreateIndex(
                name: "IX_JunctionElements_BumperVariantId",
                table: "JunctionElements",
                column: "BumperVariantId"
            );

            migrationBuilder.CreateIndex(
                name: "IX_BumperAssets_MediaAssetId",
                table: "BumperAssets",
                column: "MediaAssetId"
            );

            migrationBuilder.CreateIndex(
                name: "IX_BumperAssets_Signature",
                table: "BumperAssets",
                column: "Signature",
                unique: true
            );

            migrationBuilder.CreateIndex(
                name: "IX_BumperTemplates_Name",
                table: "BumperTemplates",
                column: "Name"
            );

            migrationBuilder.AddForeignKey(
                name: "FK_BumperTextVariants_BumperTemplates_BumperTemplateId",
                table: "BumperTextVariants",
                column: "BumperTemplateId",
                principalTable: "BumperTemplates",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade
            );

            migrationBuilder.AddForeignKey(
                name: "FK_JunctionElements_BumperTemplates_BumperTemplateId",
                table: "JunctionElements",
                column: "BumperTemplateId",
                principalTable: "BumperTemplates",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict
            );

            migrationBuilder.AddForeignKey(
                name: "FK_JunctionElements_BumperTextVariants_BumperVariantId",
                table: "JunctionElements",
                column: "BumperVariantId",
                principalTable: "BumperTextVariants",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict
            );
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_BumperTextVariants_BumperTemplates_BumperTemplateId",
                table: "BumperTextVariants"
            );

            migrationBuilder.DropForeignKey(
                name: "FK_JunctionElements_BumperTemplates_BumperTemplateId",
                table: "JunctionElements"
            );

            migrationBuilder.DropForeignKey(
                name: "FK_JunctionElements_BumperTextVariants_BumperVariantId",
                table: "JunctionElements"
            );

            migrationBuilder.DropIndex(
                name: "IX_JunctionTemplates_Name",
                table: "JunctionTemplates"
            );

            migrationBuilder.DropIndex(
                name: "IX_JunctionElements_BumperTemplateId",
                table: "JunctionElements"
            );

            migrationBuilder.DropIndex(
                name: "IX_JunctionElements_BumperVariantId",
                table: "JunctionElements"
            );

            migrationBuilder.DropIndex(name: "IX_BumperAssets_MediaAssetId", table: "BumperAssets");

            migrationBuilder.DropIndex(name: "IX_BumperAssets_Signature", table: "BumperAssets");

            migrationBuilder.DropPrimaryKey(name: "PK_BumperTemplates", table: "BumperTemplates");

            migrationBuilder.DropIndex(name: "IX_BumperTemplates_Name", table: "BumperTemplates");

            migrationBuilder.DropColumn(name: "MaxTotalSeconds", table: "JunctionTemplates");

            migrationBuilder.DropColumn(name: "BumperVariantId", table: "JunctionElements");

            migrationBuilder.DropColumn(name: "ChoiceKey", table: "JunctionElements");

            migrationBuilder.DropColumn(name: "ChoiceWeight", table: "JunctionElements");

            migrationBuilder.DropColumn(name: "Lines", table: "BumperTextVariants");

            migrationBuilder.DropColumn(name: "PosterShowId", table: "BumperAssets");

            migrationBuilder.DropColumn(name: "RenderedLinesJson", table: "BumperAssets");

            migrationBuilder.RenameTable(name: "BumperTemplates", newName: "BumperTemplate");

            migrationBuilder.DropColumn(name: "Background", table: "BumperTextVariants");

            migrationBuilder.AddColumn<int>(
                name: "Kind",
                table: "BumperTextVariants",
                type: "integer",
                nullable: false,
                defaultValue: 0
            );

            migrationBuilder.DropColumn(name: "Font", table: "BumperTemplate");

            migrationBuilder.AddColumn<int>(
                name: "Position",
                table: "BumperTemplate",
                type: "integer",
                nullable: false,
                defaultValue: 0
            );

            migrationBuilder.AddColumn<Guid>(
                name: "ChannelId",
                table: "JunctionTemplates",
                type: "uuid",
                nullable: false,
                defaultValue: new Guid("00000000-0000-0000-0000-000000000000")
            );

            migrationBuilder.AddColumn<int>(
                name: "BumperFont",
                table: "Channels",
                type: "integer",
                nullable: false,
                defaultValue: 0
            );

            migrationBuilder.AddColumn<int>(
                name: "BumperSelection",
                table: "Channels",
                type: "integer",
                nullable: false,
                defaultValue: 0
            );

            migrationBuilder.AddColumn<bool>(
                name: "BumpersEnabled",
                table: "Channels",
                type: "boolean",
                nullable: false,
                defaultValue: false
            );

            migrationBuilder.AddColumn<string>(
                name: "Line1",
                table: "BumperTextVariants",
                type: "character varying(120)",
                maxLength: 120,
                nullable: false,
                defaultValue: ""
            );

            migrationBuilder.AddColumn<string>(
                name: "Line2",
                table: "BumperTextVariants",
                type: "character varying(120)",
                maxLength: 120,
                nullable: false,
                defaultValue: ""
            );

            migrationBuilder.AddColumn<string>(
                name: "NextLabel",
                table: "BumperTextVariants",
                type: "character varying(64)",
                maxLength: 64,
                nullable: false,
                defaultValue: ""
            );

            migrationBuilder.AddColumn<string>(
                name: "NowLabel",
                table: "BumperTextVariants",
                type: "character varying(64)",
                maxLength: 64,
                nullable: false,
                defaultValue: ""
            );

            migrationBuilder.AddColumn<Guid>(
                name: "ChannelId",
                table: "BumperAssets",
                type: "uuid",
                nullable: false,
                defaultValue: new Guid("00000000-0000-0000-0000-000000000000")
            );

            migrationBuilder.AddColumn<Guid>(
                name: "FromShowId",
                table: "BumperAssets",
                type: "uuid",
                nullable: false,
                defaultValue: new Guid("00000000-0000-0000-0000-000000000000")
            );

            migrationBuilder.AddColumn<Guid>(
                name: "ToShowId",
                table: "BumperAssets",
                type: "uuid",
                nullable: false,
                defaultValue: new Guid("00000000-0000-0000-0000-000000000000")
            );

            migrationBuilder.AddColumn<Guid>(
                name: "ChannelId",
                table: "BumperTemplate",
                type: "uuid",
                nullable: false,
                defaultValue: new Guid("00000000-0000-0000-0000-000000000000")
            );

            migrationBuilder.AddPrimaryKey(
                name: "PK_BumperTemplate",
                table: "BumperTemplate",
                column: "Id"
            );

            migrationBuilder.CreateIndex(
                name: "IX_JunctionTemplates_ChannelId",
                table: "JunctionTemplates",
                column: "ChannelId"
            );

            migrationBuilder.CreateIndex(
                name: "IX_BumperAssets_FromShowId_ToShowId_Signature",
                table: "BumperAssets",
                columns: new[] { "FromShowId", "ToShowId", "Signature" }
            );

            migrationBuilder.CreateIndex(
                name: "IX_BumperTemplate_ChannelId_Position",
                table: "BumperTemplate",
                columns: new[] { "ChannelId", "Position" }
            );

            migrationBuilder.AddForeignKey(
                name: "FK_BumperTemplate_Channels_ChannelId",
                table: "BumperTemplate",
                column: "ChannelId",
                principalTable: "Channels",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade
            );

            migrationBuilder.AddForeignKey(
                name: "FK_BumperTextVariants_BumperTemplate_BumperTemplateId",
                table: "BumperTextVariants",
                column: "BumperTemplateId",
                principalTable: "BumperTemplate",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade
            );
        }
    }
}
