using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace PARR.DAL.Migrations
{
    /// <inheritdoc />
    public partial class tblUnitsInTemplates : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropIndex(
                name: "IX_Templates_Name",
                table: "Templates");

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

            migrationBuilder.AddColumn<bool>(
                name: "IsUnused",
                table: "Templates",
                type: "boolean",
                nullable: false,
                defaultValue: false);

            migrationBuilder.CreateTable(
                name: "UnitsInTemplates",
                schema: "job",
                columns: table => new
                {
                    TemplateId = table.Column<Guid>(type: "uuid", nullable: false),
                    UnitId = table.Column<Guid>(type: "uuid", nullable: false),
                    DateCreated = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_UnitsInTemplates", x => new { x.TemplateId, x.UnitId });
                    table.ForeignKey(
                        name: "FK_UnitsInTemplates_Templates_TemplateId",
                        column: x => x.TemplateId,
                        principalTable: "Templates",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_UnitsInTemplates_Units_UnitId",
                        column: x => x.UnitId,
                        principalSchema: "unit",
                        principalTable: "Units",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "Таблица связи ЭК в шаблонах");

            migrationBuilder.CreateIndex(
                name: "IX_Templates_Name_Index",
                table: "Templates",
                columns: new[] { "Name", "Index" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_UnitsInTemplates_UnitId",
                schema: "job",
                table: "UnitsInTemplates",
                column: "UnitId");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "UnitsInTemplates",
                schema: "job");

            migrationBuilder.DropIndex(
                name: "IX_Templates_Name_Index",
                table: "Templates");

            migrationBuilder.DropColumn(
                name: "Index",
                table: "Templates");

            migrationBuilder.DropColumn(
                name: "IsUnused",
                table: "Templates");

            migrationBuilder.CreateIndex(
                name: "IX_Templates_Name",
                table: "Templates",
                column: "Name",
                unique: true);
        }
    }
}
