using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

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

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

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

            migrationBuilder.CreateTable(
                name: "JunctionTemplates",
                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(128)",
                        maxLength: 128,
                        nullable: false
                    ),
                    CreatedAt = table.Column<DateTimeOffset>(
                        type: "timestamp with time zone",
                        nullable: false
                    ),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_JunctionTemplates", x => x.Id);
                }
            );

            migrationBuilder.CreateTable(
                name: "JunctionElements",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    JunctionTemplateId = table.Column<Guid>(type: "uuid", nullable: false),
                    Position = table.Column<int>(type: "integer", nullable: false),
                    Kind = table.Column<int>(type: "integer", nullable: false),
                    GroupId = table.Column<Guid>(type: "uuid", nullable: true),
                    BumperTemplateId = table.Column<Guid>(type: "uuid", nullable: true),
                    AmountMode = table.Column<int>(type: "integer", nullable: false),
                    AmountValue = table.Column<int>(type: "integer", nullable: false),
                    IsRequired = table.Column<bool>(type: "boolean", nullable: false),
                    ConditionsJson = table.Column<string>(type: "jsonb", nullable: true),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_JunctionElements", x => x.Id);
                    table.ForeignKey(
                        name: "FK_JunctionElements_Groups_GroupId",
                        column: x => x.GroupId,
                        principalTable: "Groups",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict
                    );
                    table.ForeignKey(
                        name: "FK_JunctionElements_JunctionTemplates_JunctionTemplateId",
                        column: x => x.JunctionTemplateId,
                        principalTable: "JunctionTemplates",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade
                    );
                }
            );

            migrationBuilder.CreateIndex(
                name: "IX_JunctionElements_GroupId",
                table: "JunctionElements",
                column: "GroupId"
            );

            migrationBuilder.CreateIndex(
                name: "IX_JunctionElements_JunctionTemplateId_Position",
                table: "JunctionElements",
                columns: new[] { "JunctionTemplateId", "Position" }
            );

            migrationBuilder.CreateIndex(
                name: "IX_JunctionTemplates_ChannelId",
                table: "JunctionTemplates",
                column: "ChannelId"
            );
        }

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

            migrationBuilder.DropTable(name: "JunctionTemplates");

            migrationBuilder.DropColumn(name: "JunctionAfterId", table: "Slots");

            migrationBuilder.DropColumn(name: "JunctionBetweenId", table: "Slots");

            migrationBuilder.DropColumn(name: "DefaultJunctionId", table: "ScheduleTemplates");
        }
    }
}
