using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace TeleWave.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class BumperTextVariants : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "BumperTextVariant",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    BumperTemplateId = table.Column<Guid>(type: "uuid", nullable: false),
                    Position = table.Column<int>(type: "integer", nullable: false),
                    Name = table.Column<string>(
                        type: "character varying(64)",
                        maxLength: 64,
                        nullable: false
                    ),
                    Kind = table.Column<int>(type: "integer", nullable: false),
                    NowLabel = table.Column<string>(
                        type: "character varying(64)",
                        maxLength: 64,
                        nullable: false
                    ),
                    NextLabel = table.Column<string>(
                        type: "character varying(64)",
                        maxLength: 64,
                        nullable: false
                    ),
                    Line1 = table.Column<string>(
                        type: "character varying(120)",
                        maxLength: 120,
                        nullable: false
                    ),
                    Line2 = table.Column<string>(
                        type: "character varying(120)",
                        maxLength: 120,
                        nullable: false
                    ),
                    Trigger = table.Column<int>(type: "integer", nullable: false),
                    CreatedAt = table.Column<DateTimeOffset>(
                        type: "timestamp with time zone",
                        nullable: false
                    ),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_BumperTextVariant", x => x.Id);
                    table.ForeignKey(
                        name: "FK_BumperTextVariant_BumperTemplate_BumperTemplateId",
                        column: x => x.BumperTemplateId,
                        principalTable: "BumperTemplate",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade
                    );
                }
            );

            migrationBuilder.CreateIndex(
                name: "IX_BumperTextVariant_BumperTemplateId_Position",
                table: "BumperTextVariant",
                columns: new[] { "BumperTemplateId", "Position" }
            );

            // Каждому блоку — дефолтный подблок «Сейчас/Далее» с прежними подписями канала; правило
            // показа переносим из старой галочки (только на смене шоу → OnShowChange=0, иначе Both=2).
            migrationBuilder.Sql(
                """
                DO $$
                DECLARE r RECORD;
                BEGIN
                  FOR r IN
                    SELECT t."Id" AS tid, c."BumperNowLabel" AS nl, c."BumperNextLabel" AS xl,
                           c."BumperOnlyBetweenDifferentShows" AS only_diff
                    FROM "BumperTemplate" t
                    JOIN "Channels" c ON c."Id" = t."ChannelId"
                  LOOP
                    INSERT INTO "BumperTextVariant"
                      ("Id", "BumperTemplateId", "Position", "Name", "Kind",
                       "NowLabel", "NextLabel", "Line1", "Line2", "Trigger", "CreatedAt")
                    VALUES (gen_random_uuid(), r.tid, 0, 'Текст 1', 0,
                            COALESCE(NULLIF(r.nl, ''), 'СЕЙЧАС'), COALESCE(NULLIF(r.xl, ''), 'ДАЛЕЕ'),
                            '', '', CASE WHEN r.only_diff THEN 0 ELSE 2 END, now());
                  END LOOP;
                END $$;
                """
            );

            migrationBuilder.DropColumn(name: "BumperNextLabel", table: "Channels");
            migrationBuilder.DropColumn(name: "BumperNowLabel", table: "Channels");
            migrationBuilder.DropColumn(name: "BumperOnlyBetweenDifferentShows", table: "Channels");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(name: "BumperTextVariant");

            migrationBuilder.AddColumn<string>(
                name: "BumperNextLabel",
                table: "Channels",
                type: "text",
                nullable: false,
                defaultValue: ""
            );

            migrationBuilder.AddColumn<string>(
                name: "BumperNowLabel",
                table: "Channels",
                type: "text",
                nullable: false,
                defaultValue: ""
            );

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