using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace TeleWave.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class BumperTemplates : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(name: "ChannelJingle");

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

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

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

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

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

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

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

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

            migrationBuilder.RenameColumn(
                name: "NextJingleIndex",
                table: "Channels",
                newName: "NextBumperIndex"
            );

            // Ревизия файлов уехала на блоки заставок — старый счётчик не переносим, стратегия по
            // умолчанию Rotation (0).
            migrationBuilder.DropColumn(name: "BumperRevision", table: "Channels");

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

            migrationBuilder.CreateTable(
                name: "BumperTemplate",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    ChannelId = 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
                    ),
                    BackgroundColor = table.Column<string>(
                        type: "character varying(32)",
                        maxLength: 32,
                        nullable: false
                    ),
                    BackgroundColor2 = table.Column<string>(
                        type: "character varying(32)",
                        maxLength: 32,
                        nullable: false
                    ),
                    AccentColor = table.Column<string>(
                        type: "character varying(32)",
                        maxLength: 32,
                        nullable: false
                    ),
                    TextColor = table.Column<string>(
                        type: "character varying(32)",
                        maxLength: 32,
                        nullable: false
                    ),
                    BackgroundImageExtension = table.Column<string>(
                        type: "character varying(16)",
                        maxLength: 16,
                        nullable: true
                    ),
                    AudioExtension = table.Column<string>(
                        type: "character varying(16)",
                        maxLength: 16,
                        nullable: true
                    ),
                    AudioDurationSeconds = table.Column<double>(
                        type: "double precision",
                        nullable: true
                    ),
                    Revision = table.Column<int>(type: "integer", nullable: false),
                    CreatedAt = table.Column<DateTimeOffset>(
                        type: "timestamp with time zone",
                        nullable: false
                    ),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_BumperTemplate", x => x.Id);
                    table.ForeignKey(
                        name: "FK_BumperTemplate_Channels_ChannelId",
                        column: x => x.ChannelId,
                        principalTable: "Channels",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade
                    );
                }
            );

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

            // Каждому существующему каналу — дефолтный блок заставки (Position 0, без звука/фона).
            migrationBuilder.Sql(
                """
                INSERT INTO "BumperTemplate"
                    ("Id", "ChannelId", "Position", "Name", "BackgroundColor", "BackgroundColor2",
                     "AccentColor", "TextColor", "Revision", "CreatedAt")
                SELECT gen_random_uuid(), c."Id", 0, 'Заставка 1', '0x0b1020', '0x1e293b',
                       '0x38bdf8', 'white', 0, now()
                FROM "Channels" c;
                """
            );
        }

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

            migrationBuilder.RenameColumn(
                name: "NextBumperIndex",
                table: "Channels",
                newName: "NextJingleIndex"
            );

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

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

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

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

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

            migrationBuilder.AddColumn<string>(
                name: "BumperBackgroundExtension",
                table: "Channels",
                type: "text",
                nullable: true
            );

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

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

            migrationBuilder.AddColumn<string>(
                name: "BumperMusicExtension",
                table: "Channels",
                type: "text",
                nullable: true
            );

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

            migrationBuilder.CreateTable(
                name: "ChannelJingle",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    ChannelId = table.Column<Guid>(type: "uuid", nullable: false),
                    MediaAssetId = table.Column<Guid>(type: "uuid", nullable: false),
                    Position = table.Column<int>(type: "integer", nullable: false),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ChannelJingle", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ChannelJingle_Channels_ChannelId",
                        column: x => x.ChannelId,
                        principalTable: "Channels",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade
                    );
                }
            );

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