using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace TeleWave.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class EpisodeStillAndBumperBgImages : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<Guid>(
                name: "StillImageId",
                table: "ShowEpisode",
                type: "uuid",
                nullable: true
            );

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

            // Кадры серий → реестр (Category=2 EpisodeStill); файлы перекладывает RelocateLegacyImagesAsync.
            migrationBuilder.Sql(
                """
                DO $$
                DECLARE r RECORD; img uuid;
                BEGIN
                  FOR r IN SELECT "Id", "StillPath" FROM "ShowEpisode" WHERE "StillPath" IS NOT NULL LOOP
                    img := gen_random_uuid();
                    INSERT INTO "Images" ("Id", "Category", "FileExtension", "OriginalFileName", "CreatedAt")
                    VALUES (img, 2, lower(coalesce(substring(r."StillPath" from '\.[^.]*$'), '.jpg')), 'still', now());
                    UPDATE "ShowEpisode" SET "StillImageId" = img WHERE "Id" = r."Id";
                  END LOOP;
                END $$;
                """
            );

            // Фоны блоков заставок → реестр (Category=3 BumperBackground).
            migrationBuilder.Sql(
                """
                DO $$
                DECLARE r RECORD; img uuid;
                BEGIN
                  FOR r IN SELECT "Id", "BackgroundImageExtension" FROM "BumperTemplate" WHERE "BackgroundImageExtension" IS NOT NULL LOOP
                    img := gen_random_uuid();
                    INSERT INTO "Images" ("Id", "Category", "FileExtension", "OriginalFileName", "CreatedAt")
                    VALUES (img, 3, lower(r."BackgroundImageExtension"), 'background', now());
                    UPDATE "BumperTemplate" SET "BackgroundImageId" = img WHERE "Id" = r."Id";
                  END LOOP;
                END $$;
                """
            );

            migrationBuilder.DropColumn(name: "StillPath", table: "ShowEpisode");

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

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(name: "StillImageId", table: "ShowEpisode");

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

            migrationBuilder.AddColumn<string>(
                name: "StillPath",
                table: "ShowEpisode",
                type: "character varying(256)",
                maxLength: 256,
                nullable: true
            );

            migrationBuilder.AddColumn<string>(
                name: "BackgroundImageExtension",
                table: "BumperTemplate",
                type: "character varying(16)",
                maxLength: 16,
                nullable: true
            );
        }
    }
}
