using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace TeleWave.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AddScheduleTemplate : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<TimeOnly>(
                name: "DayStartTime",
                table: "Channels",
                type: "time without time zone",
                nullable: false,
                defaultValue: new TimeOnly(0, 0, 0)
            );

            migrationBuilder.AddColumn<int>(
                name: "Number",
                table: "Channels",
                type: "integer",
                nullable: true
            );

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

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

            migrationBuilder.CreateTable(
                name: "ScheduleTemplates",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    ChannelId = table.Column<Guid>(type: "uuid", nullable: false),
                    Name = table.Column<string>(
                        type: "character varying(256)",
                        maxLength: 256,
                        nullable: false
                    ),
                    FallbackGroupId = table.Column<Guid>(type: "uuid", nullable: true),
                    Revision = table.Column<int>(type: "integer", nullable: false),
                    AppliedRevision = table.Column<int>(type: "integer", nullable: false),
                    CreatedAt = table.Column<DateTimeOffset>(
                        type: "timestamp with time zone",
                        nullable: false
                    ),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ScheduleTemplates", x => x.Id);
                }
            );

            migrationBuilder.CreateTable(
                name: "GridLayers",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    TemplateId = table.Column<Guid>(type: "uuid", nullable: false),
                    Name = table.Column<string>(
                        type: "character varying(128)",
                        maxLength: 128,
                        nullable: false
                    ),
                    Priority = table.Column<int>(type: "integer", nullable: false),
                    ApplicabilityJson = table.Column<string>(type: "jsonb", nullable: true),
                    IsEnabled = table.Column<bool>(type: "boolean", nullable: false),
                    IsBackground = table.Column<bool>(type: "boolean", nullable: false),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_GridLayers", x => x.Id);
                    table.ForeignKey(
                        name: "FK_GridLayers_ScheduleTemplates_TemplateId",
                        column: x => x.TemplateId,
                        principalTable: "ScheduleTemplates",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade
                    );
                }
            );

            migrationBuilder.CreateTable(
                name: "Slots",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    LayerId = table.Column<Guid>(type: "uuid", nullable: false),
                    Weekday = table.Column<int>(type: "integer", nullable: true),
                    TargetStart = table.Column<TimeOnly>(
                        type: "time without time zone",
                        nullable: false
                    ),
                    TargetDurationMinutes = table.Column<int>(type: "integer", nullable: false),
                    Title = table.Column<string>(
                        type: "character varying(256)",
                        maxLength: 256,
                        nullable: false
                    ),
                    Daypart = table.Column<int>(type: "integer", nullable: false),
                    SlotKind = table.Column<int>(type: "integer", nullable: false),
                    GroupId = table.Column<Guid>(type: "uuid", nullable: true),
                    StrategyJson = table.Column<string>(type: "jsonb", nullable: true),
                    RepeatSourceJson = table.Column<string>(type: "jsonb", nullable: true),
                    BlockMode = table.Column<int>(type: "integer", nullable: false),
                    BlockValue = table.Column<int>(type: "integer", nullable: false),
                    OverflowPolicy = table.Column<int>(type: "integer", nullable: false),
                    IsAnchor = table.Column<bool>(type: "boolean", nullable: false),
                    MaxDriftMinutes = table.Column<int>(type: "integer", nullable: false),
                    SnapToMinutes = table.Column<int>(type: "integer", nullable: true),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Slots", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Slots_GridLayers_LayerId",
                        column: x => x.LayerId,
                        principalTable: "GridLayers",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade
                    );
                    table.ForeignKey(
                        name: "FK_Slots_Groups_GroupId",
                        column: x => x.GroupId,
                        principalTable: "Groups",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict
                    );
                }
            );

            migrationBuilder.CreateTable(
                name: "SlotStates",
                columns: table => new
                {
                    SlotId = table.Column<Guid>(type: "uuid", nullable: false),
                    CurrentElementKind = table.Column<int>(type: "integer", nullable: true),
                    CurrentElementId = table.Column<Guid>(type: "uuid", nullable: true),
                    NextUnitIndex = table.Column<int>(type: "integer", nullable: false),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_SlotStates", x => x.SlotId);
                    table.ForeignKey(
                        name: "FK_SlotStates_Slots_SlotId",
                        column: x => x.SlotId,
                        principalTable: "Slots",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade
                    );
                }
            );

            migrationBuilder.CreateIndex(
                name: "IX_Channels_Number",
                table: "Channels",
                column: "Number",
                unique: true,
                filter: "\"Number\" IS NOT NULL"
            );

            migrationBuilder.CreateIndex(
                name: "IX_GridLayers_TemplateId_Priority",
                table: "GridLayers",
                columns: new[] { "TemplateId", "Priority" }
            );

            migrationBuilder.CreateIndex(
                name: "IX_ScheduleTemplates_ChannelId",
                table: "ScheduleTemplates",
                column: "ChannelId"
            );

            migrationBuilder.CreateIndex(
                name: "IX_Slots_GroupId",
                table: "Slots",
                column: "GroupId"
            );

            migrationBuilder.CreateIndex(
                name: "IX_Slots_LayerId_TargetStart",
                table: "Slots",
                columns: new[] { "LayerId", "TargetStart" }
            );
        }

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

            migrationBuilder.DropTable(name: "Slots");

            migrationBuilder.DropTable(name: "GridLayers");

            migrationBuilder.DropTable(name: "ScheduleTemplates");

            migrationBuilder.DropIndex(name: "IX_Channels_Number", table: "Channels");

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

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

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

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