using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace TeleWave.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AddBroadcastAndLibrary : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Channels",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    Name = table.Column<string>(
                        type: "character varying(256)",
                        maxLength: 256,
                        nullable: false
                    ),
                    Slug = table.Column<string>(
                        type: "character varying(128)",
                        maxLength: 128,
                        nullable: false
                    ),
                    IsEnabled = table.Column<bool>(type: "boolean", nullable: false),
                    EpochUtc = table.Column<DateTimeOffset>(
                        type: "timestamp with time zone",
                        nullable: false
                    ),
                    AdInsertion = table.Column<int>(type: "integer", nullable: false),
                    AdsPerBreak = table.Column<int>(type: "integer", nullable: false),
                    FillerAssetId = table.Column<Guid>(type: "uuid", nullable: true),
                    NextAdIndex = table.Column<int>(type: "integer", nullable: false),
                    CreatedAt = table.Column<DateTimeOffset>(
                        type: "timestamp with time zone",
                        nullable: false
                    ),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Channels", x => x.Id);
                }
            );

            migrationBuilder.CreateTable(
                name: "ScheduleEntries",
                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),
                    Kind = table.Column<int>(type: "integer", nullable: false),
                    StartsAtUtc = table.Column<DateTimeOffset>(
                        type: "timestamp with time zone",
                        nullable: false
                    ),
                    EndsAtUtc = table.Column<DateTimeOffset>(
                        type: "timestamp with time zone",
                        nullable: false
                    ),
                    ShowId = table.Column<Guid>(type: "uuid", nullable: true),
                    EpisodeIndex = table.Column<int>(type: "integer", nullable: true),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ScheduleEntries", x => x.Id);
                }
            );

            migrationBuilder.CreateTable(
                name: "Shows",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    Name = table.Column<string>(
                        type: "character varying(256)",
                        maxLength: 256,
                        nullable: false
                    ),
                    Description = table.Column<string>(
                        type: "character varying(2048)",
                        maxLength: 2048,
                        nullable: true
                    ),
                    Kind = table.Column<int>(type: "integer", nullable: false),
                    CreatedAt = table.Column<DateTimeOffset>(
                        type: "timestamp with time zone",
                        nullable: false
                    ),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Shows", x => x.Id);
                }
            );

            migrationBuilder.CreateTable(
                name: "ChannelAd",
                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_ChannelAd", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ChannelAd_Channels_ChannelId",
                        column: x => x.ChannelId,
                        principalTable: "Channels",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade
                    );
                }
            );

            migrationBuilder.CreateTable(
                name: "ChannelShow",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    ChannelId = table.Column<Guid>(type: "uuid", nullable: false),
                    ShowId = table.Column<Guid>(type: "uuid", nullable: false),
                    Weight = table.Column<int>(type: "integer", nullable: false),
                    BlockMode = table.Column<int>(type: "integer", nullable: false),
                    BlockValue = table.Column<int>(type: "integer", nullable: false),
                    IsEnabled = table.Column<bool>(type: "boolean", nullable: false),
                    NextEpisodeIndex = table.Column<int>(type: "integer", nullable: false),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ChannelShow", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ChannelShow_Channels_ChannelId",
                        column: x => x.ChannelId,
                        principalTable: "Channels",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade
                    );
                }
            );

            migrationBuilder.CreateTable(
                name: "ProgrammingOverride",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    ChannelId = table.Column<Guid>(type: "uuid", nullable: false),
                    Mode = table.Column<int>(type: "integer", nullable: false),
                    StartsAtUtc = table.Column<DateTimeOffset>(
                        type: "timestamp with time zone",
                        nullable: false
                    ),
                    EndsAtUtc = table.Column<DateTimeOffset>(
                        type: "timestamp with time zone",
                        nullable: false
                    ),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ProgrammingOverride", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ProgrammingOverride_Channels_ChannelId",
                        column: x => x.ChannelId,
                        principalTable: "Channels",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade
                    );
                }
            );

            migrationBuilder.CreateTable(
                name: "ShowEpisode",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    ShowId = table.Column<Guid>(type: "uuid", nullable: false),
                    MediaAssetId = table.Column<Guid>(type: "uuid", nullable: false),
                    Position = table.Column<int>(type: "integer", nullable: false),
                    CreatedAt = table.Column<DateTimeOffset>(
                        type: "timestamp with time zone",
                        nullable: false
                    ),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ShowEpisode", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ShowEpisode_Shows_ShowId",
                        column: x => x.ShowId,
                        principalTable: "Shows",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade
                    );
                }
            );

            migrationBuilder.CreateTable(
                name: "OverrideShow",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    ProgrammingOverrideId = table.Column<Guid>(type: "uuid", nullable: false),
                    ShowId = table.Column<Guid>(type: "uuid", nullable: false),
                    Weight = table.Column<int>(type: "integer", nullable: false),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_OverrideShow", x => x.Id);
                    table.ForeignKey(
                        name: "FK_OverrideShow_ProgrammingOverride_ProgrammingOverrideId",
                        column: x => x.ProgrammingOverrideId,
                        principalTable: "ProgrammingOverride",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade
                    );
                }
            );

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

            migrationBuilder.CreateIndex(
                name: "IX_Channels_Slug",
                table: "Channels",
                column: "Slug",
                unique: true
            );

            migrationBuilder.CreateIndex(
                name: "IX_ChannelShow_ChannelId_ShowId",
                table: "ChannelShow",
                columns: new[] { "ChannelId", "ShowId" }
            );

            migrationBuilder.CreateIndex(
                name: "IX_OverrideShow_ProgrammingOverrideId",
                table: "OverrideShow",
                column: "ProgrammingOverrideId"
            );

            migrationBuilder.CreateIndex(
                name: "IX_ProgrammingOverride_ChannelId_StartsAtUtc_EndsAtUtc",
                table: "ProgrammingOverride",
                columns: new[] { "ChannelId", "StartsAtUtc", "EndsAtUtc" }
            );

            migrationBuilder.CreateIndex(
                name: "IX_ScheduleEntries_ChannelId_EndsAtUtc",
                table: "ScheduleEntries",
                columns: new[] { "ChannelId", "EndsAtUtc" }
            );

            migrationBuilder.CreateIndex(
                name: "IX_ScheduleEntries_ChannelId_ShowId",
                table: "ScheduleEntries",
                columns: new[] { "ChannelId", "ShowId" }
            );

            migrationBuilder.CreateIndex(
                name: "IX_ScheduleEntries_ChannelId_StartsAtUtc",
                table: "ScheduleEntries",
                columns: new[] { "ChannelId", "StartsAtUtc" }
            );

            migrationBuilder.CreateIndex(
                name: "IX_ShowEpisode_MediaAssetId",
                table: "ShowEpisode",
                column: "MediaAssetId"
            );

            migrationBuilder.CreateIndex(
                name: "IX_ShowEpisode_ShowId_Position",
                table: "ShowEpisode",
                columns: new[] { "ShowId", "Position" }
            );
        }

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

            migrationBuilder.DropTable(name: "ChannelShow");

            migrationBuilder.DropTable(name: "OverrideShow");

            migrationBuilder.DropTable(name: "ScheduleEntries");

            migrationBuilder.DropTable(name: "ShowEpisode");

            migrationBuilder.DropTable(name: "ProgrammingOverride");

            migrationBuilder.DropTable(name: "Shows");

            migrationBuilder.DropTable(name: "Channels");
        }
    }
}
