// <copyright file="DropBaseItemFlatExtensionColumns.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>

using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Jellyfin.Database.Providers.Postgres.Migrations
{
    /// <summary>
    /// Removes the 17 flat extension columns from library.BaseItems now that all data has been
    /// migrated to the BaseItemTvExtras, BaseItemLiveTvExtras, and BaseItemAudioExtras tables.
    /// Must run AFTER SplitBaseItemExtras (20260503000000).
    /// </summary>
    public partial class DropBaseItemFlatExtensionColumns : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // ── 1. Drop indexes that reference columns being removed ───────────────
            migrationBuilder.DropIndex(
                name: "IX_BaseItems_Type_TopParentId_StartDate",
                schema: "library",
                table: "BaseItems");

            migrationBuilder.DropIndex(
                name: "IX_BaseItems_Type_SeriesPresentationUniqueKey_IsFolder_IsVirtualItem",
                schema: "library",
                table: "BaseItems");

            migrationBuilder.DropIndex(
                name: "IX_BaseItems_Type_SeriesPresentationUniqueKey_PresentationUniqueKey_SortName",
                schema: "library",
                table: "BaseItems");

            // ── 2. Drop 17 flat columns (now in extension tables) ─────────────────
            // TV-specific → BaseItemTvExtras
            migrationBuilder.DropColumn(name: "SeriesId", schema: "library", table: "BaseItems");
            migrationBuilder.DropColumn(name: "SeasonId", schema: "library", table: "BaseItems");
            migrationBuilder.DropColumn(name: "SeriesName", schema: "library", table: "BaseItems");
            migrationBuilder.DropColumn(name: "SeasonName", schema: "library", table: "BaseItems");
            migrationBuilder.DropColumn(name: "SeriesPresentationUniqueKey", schema: "library", table: "BaseItems");

            // LiveTV-specific → BaseItemLiveTvExtras
            migrationBuilder.DropColumn(name: "StartDate", schema: "library", table: "BaseItems");
            migrationBuilder.DropColumn(name: "EndDate", schema: "library", table: "BaseItems");
            migrationBuilder.DropColumn(name: "EpisodeTitle", schema: "library", table: "BaseItems");
            migrationBuilder.DropColumn(name: "ShowId", schema: "library", table: "BaseItems");
            migrationBuilder.DropColumn(name: "ExternalSeriesId", schema: "library", table: "BaseItems");
            migrationBuilder.DropColumn(name: "ExternalServiceId", schema: "library", table: "BaseItems");
            migrationBuilder.DropColumn(name: "Audio", schema: "library", table: "BaseItems");

            // Audio-specific → BaseItemAudioExtras
            migrationBuilder.DropColumn(name: "Album", schema: "library", table: "BaseItems");
            migrationBuilder.DropColumn(name: "Artists", schema: "library", table: "BaseItems");
            migrationBuilder.DropColumn(name: "AlbumArtists", schema: "library", table: "BaseItems");
            migrationBuilder.DropColumn(name: "LUFS", schema: "library", table: "BaseItems");
            migrationBuilder.DropColumn(name: "NormalizationGain", schema: "library", table: "BaseItems");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            // Re-add TV columns
            migrationBuilder.AddColumn<Guid>(name: "SeriesId", schema: "library", table: "BaseItems", type: "uuid", nullable: true);
            migrationBuilder.AddColumn<Guid>(name: "SeasonId", schema: "library", table: "BaseItems", type: "uuid", nullable: true);
            migrationBuilder.AddColumn<string>(name: "SeriesName", schema: "library", table: "BaseItems", type: "text", nullable: true);
            migrationBuilder.AddColumn<string>(name: "SeasonName", schema: "library", table: "BaseItems", type: "text", nullable: true);
            migrationBuilder.AddColumn<string>(name: "SeriesPresentationUniqueKey", schema: "library", table: "BaseItems", type: "text", nullable: true);

            // Re-add LiveTV columns
            migrationBuilder.AddColumn<DateTime>(name: "StartDate", schema: "library", table: "BaseItems", type: "timestamp with time zone", nullable: true);
            migrationBuilder.AddColumn<DateTime>(name: "EndDate", schema: "library", table: "BaseItems", type: "timestamp with time zone", nullable: true);
            migrationBuilder.AddColumn<string>(name: "EpisodeTitle", schema: "library", table: "BaseItems", type: "text", nullable: true);
            migrationBuilder.AddColumn<string>(name: "ShowId", schema: "library", table: "BaseItems", type: "text", nullable: true);
            migrationBuilder.AddColumn<string>(name: "ExternalSeriesId", schema: "library", table: "BaseItems", type: "text", nullable: true);
            migrationBuilder.AddColumn<string>(name: "ExternalServiceId", schema: "library", table: "BaseItems", type: "text", nullable: true);
            migrationBuilder.AddColumn<int>(name: "Audio", schema: "library", table: "BaseItems", type: "integer", nullable: true);

            // Re-add Audio columns
            migrationBuilder.AddColumn<string>(name: "Album", schema: "library", table: "BaseItems", type: "text", nullable: true);
            migrationBuilder.AddColumn<string>(name: "Artists", schema: "library", table: "BaseItems", type: "text", nullable: true);
            migrationBuilder.AddColumn<string>(name: "AlbumArtists", schema: "library", table: "BaseItems", type: "text", nullable: true);
            migrationBuilder.AddColumn<float>(name: "LUFS", schema: "library", table: "BaseItems", type: "real", nullable: true);
            migrationBuilder.AddColumn<float>(name: "NormalizationGain", schema: "library", table: "BaseItems", type: "real", nullable: true);

            // Re-create dropped indexes
            migrationBuilder.CreateIndex(
                name: "IX_BaseItems_Type_TopParentId_StartDate",
                schema: "library",
                table: "BaseItems",
                columns: new[] { "Type", "TopParentId", "StartDate" });

            migrationBuilder.CreateIndex(
                name: "IX_BaseItems_Type_SeriesPresentationUniqueKey_IsFolder_IsVirtualItem",
                schema: "library",
                table: "BaseItems",
                columns: new[] { "Type", "SeriesPresentationUniqueKey", "IsFolder", "IsVirtualItem" });

            migrationBuilder.CreateIndex(
                name: "IX_BaseItems_Type_SeriesPresentationUniqueKey_PresentationUniqueKey_SortName",
                schema: "library",
                table: "BaseItems",
                columns: new[] { "Type", "SeriesPresentationUniqueKey", "PresentationUniqueKey", "SortName" });
        }
    }
}
