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

using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Jellyfin.Database.Providers.Postgres.Migrations
{
    /// <inheritdoc />
    public partial class SplitMediaStreamInfos : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // Create AudioStreamDetails table
            migrationBuilder.CreateTable(
                name: "AudioStreamDetails",
                schema: "library",
                columns: table => new
                {
                    ItemId = table.Column<Guid>(type: "uuid", nullable: false),
                    StreamIndex = table.Column<int>(type: "integer", nullable: false),
                    ChannelLayout = table.Column<string>(type: "text", nullable: true),
                    Channels = table.Column<int>(type: "integer", nullable: true),
                    SampleRate = table.Column<int>(type: "integer", nullable: true),
                    IsHearingImpaired = table.Column<bool>(type: "boolean", nullable: true),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AudioStreamDetails", x => new { x.ItemId, x.StreamIndex });
                    table.ForeignKey(
                        name: "FK_AudioStreamDetails_MediaStreamInfos_ItemId_StreamIndex",
                        columns: x => new { x.ItemId, x.StreamIndex },
                        principalSchema: "library",
                        principalTable: "MediaStreamInfos",
                        principalColumns: new[] { "ItemId", "StreamIndex" },
                        onDelete: ReferentialAction.Cascade);
                });

            // Create VideoStreamDetails table
            migrationBuilder.CreateTable(
                name: "VideoStreamDetails",
                schema: "library",
                columns: table => new
                {
                    ItemId = table.Column<Guid>(type: "uuid", nullable: false),
                    StreamIndex = table.Column<int>(type: "integer", nullable: false),
                    AspectRatio = table.Column<string>(type: "text", nullable: true),
                    IsInterlaced = table.Column<bool>(type: "boolean", nullable: true),
                    Height = table.Column<int>(type: "integer", nullable: true),
                    Width = table.Column<int>(type: "integer", nullable: true),
                    AverageFrameRate = table.Column<float>(type: "real", nullable: true),
                    RealFrameRate = table.Column<float>(type: "real", nullable: true),
                    PixelFormat = table.Column<string>(type: "text", nullable: true),
                    BitDepth = table.Column<int>(type: "integer", nullable: true),
                    IsAnamorphic = table.Column<bool>(type: "boolean", nullable: true),
                    RefFrames = table.Column<int>(type: "integer", nullable: true),
                    NalLengthSize = table.Column<string>(type: "text", nullable: true),
                    ColorPrimaries = table.Column<string>(type: "text", nullable: true),
                    ColorSpace = table.Column<string>(type: "text", nullable: true),
                    ColorTransfer = table.Column<string>(type: "text", nullable: true),
                    DvVersionMajor = table.Column<int>(type: "integer", nullable: true),
                    DvVersionMinor = table.Column<int>(type: "integer", nullable: true),
                    DvProfile = table.Column<int>(type: "integer", nullable: true),
                    DvLevel = table.Column<int>(type: "integer", nullable: true),
                    RpuPresentFlag = table.Column<int>(type: "integer", nullable: true),
                    ElPresentFlag = table.Column<int>(type: "integer", nullable: true),
                    BlPresentFlag = table.Column<int>(type: "integer", nullable: true),
                    DvBlSignalCompatibilityId = table.Column<int>(type: "integer", nullable: true),
                    Rotation = table.Column<int>(type: "integer", nullable: true),
                    KeyFrames = table.Column<string>(type: "text", nullable: true),
                    Hdr10PlusPresentFlag = table.Column<bool>(type: "boolean", nullable: true),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_VideoStreamDetails", x => new { x.ItemId, x.StreamIndex });
                    table.ForeignKey(
                        name: "FK_VideoStreamDetails_MediaStreamInfos_ItemId_StreamIndex",
                        columns: x => new { x.ItemId, x.StreamIndex },
                        principalSchema: "library",
                        principalTable: "MediaStreamInfos",
                        principalColumns: new[] { "ItemId", "StreamIndex" },
                        onDelete: ReferentialAction.Cascade);
                });

            // Migrate existing audio stream data (StreamType = 0)
            migrationBuilder.Sql(@"
                INSERT INTO library.""AudioStreamDetails"" (
                    ""ItemId"", ""StreamIndex"",
                    ""ChannelLayout"", ""Channels"", ""SampleRate"", ""IsHearingImpaired""
                )
                SELECT
                    ""ItemId"", ""StreamIndex"",
                    ""ChannelLayout"", ""Channels"", ""SampleRate"", ""IsHearingImpaired""
                FROM library.""MediaStreamInfos""
                WHERE ""StreamType"" = 0;
            ");

            // Migrate existing video stream data (StreamType = 1)
            migrationBuilder.Sql(@"
                INSERT INTO library.""VideoStreamDetails"" (
                    ""ItemId"", ""StreamIndex"",
                    ""AspectRatio"", ""IsInterlaced"", ""Height"", ""Width"",
                    ""AverageFrameRate"", ""RealFrameRate"", ""PixelFormat"", ""BitDepth"",
                    ""IsAnamorphic"", ""RefFrames"", ""NalLengthSize"",
                    ""ColorPrimaries"", ""ColorSpace"", ""ColorTransfer"",
                    ""DvVersionMajor"", ""DvVersionMinor"", ""DvProfile"", ""DvLevel"",
                    ""RpuPresentFlag"", ""ElPresentFlag"", ""BlPresentFlag"", ""DvBlSignalCompatibilityId"",
                    ""Rotation"", ""KeyFrames"", ""Hdr10PlusPresentFlag""
                )
                SELECT
                    ""ItemId"", ""StreamIndex"",
                    ""AspectRatio"", ""IsInterlaced"", ""Height"", ""Width"",
                    ""AverageFrameRate"", ""RealFrameRate"", ""PixelFormat"", ""BitDepth"",
                    ""IsAnamorphic"", ""RefFrames"", ""NalLengthSize"",
                    ""ColorPrimaries"", ""ColorSpace"", ""ColorTransfer"",
                    ""DvVersionMajor"", ""DvVersionMinor"", ""DvProfile"", ""DvLevel"",
                    ""RpuPresentFlag"", ""ElPresentFlag"", ""BlPresentFlag"", ""DvBlSignalCompatibilityId"",
                    ""Rotation"", ""KeyFrames"", ""Hdr10PlusPresentFlag""
                FROM library.""MediaStreamInfos""
                WHERE ""StreamType"" = 1;
            ");

            // Drop audio-specific columns from MediaStreamInfos
            migrationBuilder.DropColumn(name: "ChannelLayout", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "Channels", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "SampleRate", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "IsHearingImpaired", schema: "library", table: "MediaStreamInfos");

            // Drop video-specific columns from MediaStreamInfos
            migrationBuilder.DropColumn(name: "AspectRatio", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "IsInterlaced", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "Height", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "Width", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "AverageFrameRate", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "RealFrameRate", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "PixelFormat", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "BitDepth", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "IsAnamorphic", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "RefFrames", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "NalLengthSize", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "ColorPrimaries", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "ColorSpace", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "ColorTransfer", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "DvVersionMajor", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "DvVersionMinor", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "DvProfile", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "DvLevel", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "RpuPresentFlag", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "ElPresentFlag", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "BlPresentFlag", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "DvBlSignalCompatibilityId", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "Rotation", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "KeyFrames", schema: "library", table: "MediaStreamInfos");
            migrationBuilder.DropColumn(name: "Hdr10PlusPresentFlag", schema: "library", table: "MediaStreamInfos");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            // Restore audio-specific columns to MediaStreamInfos
            migrationBuilder.AddColumn<string>(name: "ChannelLayout", schema: "library", table: "MediaStreamInfos", type: "text", nullable: true);
            migrationBuilder.AddColumn<int>(name: "Channels", schema: "library", table: "MediaStreamInfos", type: "integer", nullable: true);
            migrationBuilder.AddColumn<int>(name: "SampleRate", schema: "library", table: "MediaStreamInfos", type: "integer", nullable: true);
            migrationBuilder.AddColumn<bool>(name: "IsHearingImpaired", schema: "library", table: "MediaStreamInfos", type: "boolean", nullable: true);

            // Restore video-specific columns to MediaStreamInfos
            migrationBuilder.AddColumn<string>(name: "AspectRatio", schema: "library", table: "MediaStreamInfos", type: "text", nullable: true);
            migrationBuilder.AddColumn<bool>(name: "IsInterlaced", schema: "library", table: "MediaStreamInfos", type: "boolean", nullable: true);
            migrationBuilder.AddColumn<int>(name: "Height", schema: "library", table: "MediaStreamInfos", type: "integer", nullable: true);
            migrationBuilder.AddColumn<int>(name: "Width", schema: "library", table: "MediaStreamInfos", type: "integer", nullable: true);
            migrationBuilder.AddColumn<float>(name: "AverageFrameRate", schema: "library", table: "MediaStreamInfos", type: "real", nullable: true);
            migrationBuilder.AddColumn<float>(name: "RealFrameRate", schema: "library", table: "MediaStreamInfos", type: "real", nullable: true);
            migrationBuilder.AddColumn<string>(name: "PixelFormat", schema: "library", table: "MediaStreamInfos", type: "text", nullable: true);
            migrationBuilder.AddColumn<int>(name: "BitDepth", schema: "library", table: "MediaStreamInfos", type: "integer", nullable: true);
            migrationBuilder.AddColumn<bool>(name: "IsAnamorphic", schema: "library", table: "MediaStreamInfos", type: "boolean", nullable: true);
            migrationBuilder.AddColumn<int>(name: "RefFrames", schema: "library", table: "MediaStreamInfos", type: "integer", nullable: true);
            migrationBuilder.AddColumn<string>(name: "NalLengthSize", schema: "library", table: "MediaStreamInfos", type: "text", nullable: true);
            migrationBuilder.AddColumn<string>(name: "ColorPrimaries", schema: "library", table: "MediaStreamInfos", type: "text", nullable: true);
            migrationBuilder.AddColumn<string>(name: "ColorSpace", schema: "library", table: "MediaStreamInfos", type: "text", nullable: true);
            migrationBuilder.AddColumn<string>(name: "ColorTransfer", schema: "library", table: "MediaStreamInfos", type: "text", nullable: true);
            migrationBuilder.AddColumn<int>(name: "DvVersionMajor", schema: "library", table: "MediaStreamInfos", type: "integer", nullable: true);
            migrationBuilder.AddColumn<int>(name: "DvVersionMinor", schema: "library", table: "MediaStreamInfos", type: "integer", nullable: true);
            migrationBuilder.AddColumn<int>(name: "DvProfile", schema: "library", table: "MediaStreamInfos", type: "integer", nullable: true);
            migrationBuilder.AddColumn<int>(name: "DvLevel", schema: "library", table: "MediaStreamInfos", type: "integer", nullable: true);
            migrationBuilder.AddColumn<int>(name: "RpuPresentFlag", schema: "library", table: "MediaStreamInfos", type: "integer", nullable: true);
            migrationBuilder.AddColumn<int>(name: "ElPresentFlag", schema: "library", table: "MediaStreamInfos", type: "integer", nullable: true);
            migrationBuilder.AddColumn<int>(name: "BlPresentFlag", schema: "library", table: "MediaStreamInfos", type: "integer", nullable: true);
            migrationBuilder.AddColumn<int>(name: "DvBlSignalCompatibilityId", schema: "library", table: "MediaStreamInfos", type: "integer", nullable: true);
            migrationBuilder.AddColumn<int>(name: "Rotation", schema: "library", table: "MediaStreamInfos", type: "integer", nullable: true);
            migrationBuilder.AddColumn<string>(name: "KeyFrames", schema: "library", table: "MediaStreamInfos", type: "text", nullable: true);
            migrationBuilder.AddColumn<bool>(name: "Hdr10PlusPresentFlag", schema: "library", table: "MediaStreamInfos", type: "boolean", nullable: true);

            // Restore audio data back into MediaStreamInfos
            migrationBuilder.Sql(@"
                UPDATE library.""MediaStreamInfos"" m
                SET
                    ""ChannelLayout"" = a.""ChannelLayout"",
                    ""Channels""      = a.""Channels"",
                    ""SampleRate""    = a.""SampleRate"",
                    ""IsHearingImpaired"" = a.""IsHearingImpaired""
                FROM library.""AudioStreamDetails"" a
                WHERE m.""ItemId"" = a.""ItemId"" AND m.""StreamIndex"" = a.""StreamIndex"";
            ");

            // Restore video data back into MediaStreamInfos
            migrationBuilder.Sql(@"
                UPDATE library.""MediaStreamInfos"" m
                SET
                    ""AspectRatio""   = v.""AspectRatio"",
                    ""IsInterlaced""  = v.""IsInterlaced"",
                    ""Height""        = v.""Height"",
                    ""Width""         = v.""Width"",
                    ""AverageFrameRate""    = v.""AverageFrameRate"",
                    ""RealFrameRate""       = v.""RealFrameRate"",
                    ""PixelFormat""         = v.""PixelFormat"",
                    ""BitDepth""            = v.""BitDepth"",
                    ""IsAnamorphic""        = v.""IsAnamorphic"",
                    ""RefFrames""           = v.""RefFrames"",
                    ""NalLengthSize""       = v.""NalLengthSize"",
                    ""ColorPrimaries""      = v.""ColorPrimaries"",
                    ""ColorSpace""          = v.""ColorSpace"",
                    ""ColorTransfer""       = v.""ColorTransfer"",
                    ""DvVersionMajor""      = v.""DvVersionMajor"",
                    ""DvVersionMinor""      = v.""DvVersionMinor"",
                    ""DvProfile""           = v.""DvProfile"",
                    ""DvLevel""             = v.""DvLevel"",
                    ""RpuPresentFlag""      = v.""RpuPresentFlag"",
                    ""ElPresentFlag""       = v.""ElPresentFlag"",
                    ""BlPresentFlag""       = v.""BlPresentFlag"",
                    ""DvBlSignalCompatibilityId"" = v.""DvBlSignalCompatibilityId"",
                    ""Rotation""            = v.""Rotation"",
                    ""KeyFrames""           = v.""KeyFrames"",
                    ""Hdr10PlusPresentFlag"" = v.""Hdr10PlusPresentFlag""
                FROM library.""VideoStreamDetails"" v
                WHERE m.""ItemId"" = v.""ItemId"" AND m.""StreamIndex"" = v.""StreamIndex"";
            ");

            migrationBuilder.DropTable(name: "AudioStreamDetails", schema: "library");
            migrationBuilder.DropTable(name: "VideoStreamDetails", schema: "library");
        }
    }
}
