using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional

namespace PARR.DAL.Migrations
{
    /// <inheritdoc />
    public partial class TblUpdTemplates : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_RobotConfigurations_StatusTemplates_TaskStatusCode",
                table: "RobotConfigurations");

            migrationBuilder.DropForeignKey(
                name: "FK_RobotHistories_StatusTemplates_TaskStatusCode",
                table: "RobotHistories");

            migrationBuilder.DropForeignKey(
                name: "FK_RobotTemplateHistories_StatusTemplates_TemplateStatusCode",
                table: "RobotTemplateHistories");

            migrationBuilder.DropForeignKey(
                name: "FK_Templates_RobotStatuses_RobotStatusCode",
                table: "Templates");

            migrationBuilder.DropForeignKey(
                name: "FK_Templates_StatusTemplates_StatusCode",
                table: "Templates");

            migrationBuilder.DropTable(
                name: "StatusTemplates");

            migrationBuilder.RenameColumn(
                name: "IsActive",
                table: "Templates",
                newName: "IsActiveTemplate");

            migrationBuilder.AlterColumn<int>(
                name: "StatusCode",
                table: "Templates",
                type: "integer",
                nullable: true,
                oldClrType: typeof(int),
                oldType: "integer");

            migrationBuilder.AlterColumn<int>(
                name: "RobotStatusCode",
                table: "Templates",
                type: "integer",
                nullable: true,
                oldClrType: typeof(int),
                oldType: "integer");

            migrationBuilder.AlterColumn<int>(
                name: "RobotAttemptsNumber",
                table: "Templates",
                type: "integer",
                nullable: true,
                oldClrType: typeof(int),
                oldType: "integer");

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

            migrationBuilder.CreateTable(
                name: "TaskStatuses",
                columns: table => new
                {
                    Code = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    Name = table.Column<string>(type: "text", nullable: false),
                    Description = table.Column<string>(type: "text", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_TaskStatuses", x => x.Code);
                });

            migrationBuilder.InsertData(
                table: "TaskStatuses",
                columns: new[] { "Code", "Description", "Name" },
                values: new object[,]
                {
                    { 10, "Задание на создание объекта в ЕСПП", "Creating" },
                    { 20, "Задание на обновление объекта в ЕСПП", "Updating" },
                    { 30, "Объект в ЕСПП соответствует объекту в ПАРР", "Ok" }
                });

            migrationBuilder.AddForeignKey(
                name: "FK_RobotConfigurations_TaskStatuses_TaskStatusCode",
                table: "RobotConfigurations",
                column: "TaskStatusCode",
                principalTable: "TaskStatuses",
                principalColumn: "Code",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_RobotHistories_TaskStatuses_TaskStatusCode",
                table: "RobotHistories",
                column: "TaskStatusCode",
                principalTable: "TaskStatuses",
                principalColumn: "Code",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_RobotTemplateHistories_TaskStatuses_TemplateStatusCode",
                table: "RobotTemplateHistories",
                column: "TemplateStatusCode",
                principalTable: "TaskStatuses",
                principalColumn: "Code",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_Templates_RobotStatuses_RobotStatusCode",
                table: "Templates",
                column: "RobotStatusCode",
                principalTable: "RobotStatuses",
                principalColumn: "Code");

            migrationBuilder.AddForeignKey(
                name: "FK_Templates_TaskStatuses_StatusCode",
                table: "Templates",
                column: "StatusCode",
                principalTable: "TaskStatuses",
                principalColumn: "Code");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_RobotConfigurations_TaskStatuses_TaskStatusCode",
                table: "RobotConfigurations");

            migrationBuilder.DropForeignKey(
                name: "FK_RobotHistories_TaskStatuses_TaskStatusCode",
                table: "RobotHistories");

            migrationBuilder.DropForeignKey(
                name: "FK_RobotTemplateHistories_TaskStatuses_TemplateStatusCode",
                table: "RobotTemplateHistories");

            migrationBuilder.DropForeignKey(
                name: "FK_Templates_RobotStatuses_RobotStatusCode",
                table: "Templates");

            migrationBuilder.DropForeignKey(
                name: "FK_Templates_TaskStatuses_StatusCode",
                table: "Templates");

            migrationBuilder.DropTable(
                name: "TaskStatuses");

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

            migrationBuilder.RenameColumn(
                name: "IsActiveTemplate",
                table: "Templates",
                newName: "IsActive");

            migrationBuilder.AlterColumn<int>(
                name: "StatusCode",
                table: "Templates",
                type: "integer",
                nullable: false,
                defaultValue: 0,
                oldClrType: typeof(int),
                oldType: "integer",
                oldNullable: true);

            migrationBuilder.AlterColumn<int>(
                name: "RobotStatusCode",
                table: "Templates",
                type: "integer",
                nullable: false,
                defaultValue: 0,
                oldClrType: typeof(int),
                oldType: "integer",
                oldNullable: true);

            migrationBuilder.AlterColumn<int>(
                name: "RobotAttemptsNumber",
                table: "Templates",
                type: "integer",
                nullable: false,
                defaultValue: 0,
                oldClrType: typeof(int),
                oldType: "integer",
                oldNullable: true);

            migrationBuilder.CreateTable(
                name: "StatusTemplates",
                columns: table => new
                {
                    Code = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    Description = table.Column<string>(type: "text", nullable: false),
                    Name = table.Column<string>(type: "text", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_StatusTemplates", x => x.Code);
                });

            migrationBuilder.InsertData(
                table: "StatusTemplates",
                columns: new[] { "Code", "Description", "Name" },
                values: new object[,]
                {
                    { 10, "Создали шаблон в ПАРР", "Creating" },
                    { 20, "Выявлено несоответствие. Требуется привлечение робота", "Updating" },
                    { 30, "Нормальное состояние шаблона в ЕСПП и ПАРР. Шаблон в ПАРР соответствует шаблону в ЕСПП", "Ok" }
                });

            migrationBuilder.AddForeignKey(
                name: "FK_RobotConfigurations_StatusTemplates_TaskStatusCode",
                table: "RobotConfigurations",
                column: "TaskStatusCode",
                principalTable: "StatusTemplates",
                principalColumn: "Code",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_RobotHistories_StatusTemplates_TaskStatusCode",
                table: "RobotHistories",
                column: "TaskStatusCode",
                principalTable: "StatusTemplates",
                principalColumn: "Code",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_RobotTemplateHistories_StatusTemplates_TemplateStatusCode",
                table: "RobotTemplateHistories",
                column: "TemplateStatusCode",
                principalTable: "StatusTemplates",
                principalColumn: "Code",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_Templates_RobotStatuses_RobotStatusCode",
                table: "Templates",
                column: "RobotStatusCode",
                principalTable: "RobotStatuses",
                principalColumn: "Code",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_Templates_StatusTemplates_StatusCode",
                table: "Templates",
                column: "StatusCode",
                principalTable: "StatusTemplates",
                principalColumn: "Code",
                onDelete: ReferentialAction.Cascade);
        }
    }
}
