using System;
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 TblsRobotConfigurations : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Robots",
                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_Robots", x => x.Code);
                });

            migrationBuilder.CreateTable(
                name: "RobotConfigurations",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    DateCreated = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    TemplateId = table.Column<Guid>(type: "uuid", nullable: false),
                    RobotCode = table.Column<int>(type: "integer", nullable: false),
                    TaskStatusCode = table.Column<int>(type: "integer", nullable: false),
                    RobotStatusCode = table.Column<int>(type: "integer", nullable: false),
                    AttemptsNumber = table.Column<int>(type: "integer", nullable: false),
                    LastStatusUpdated = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_RobotConfigurations", x => x.Id);
                    table.ForeignKey(
                        name: "FK_RobotConfigurations_RobotStatuses_RobotStatusCode",
                        column: x => x.RobotStatusCode,
                        principalTable: "RobotStatuses",
                        principalColumn: "Code",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_RobotConfigurations_Robots_RobotCode",
                        column: x => x.RobotCode,
                        principalTable: "Robots",
                        principalColumn: "Code",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_RobotConfigurations_StatusTemplates_TaskStatusCode",
                        column: x => x.TaskStatusCode,
                        principalTable: "StatusTemplates",
                        principalColumn: "Code",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_RobotConfigurations_Templates_TemplateId",
                        column: x => x.TemplateId,
                        principalTable: "Templates",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "RobotHistories",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    DateCreated = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    HistoryLevel = table.Column<int>(type: "integer", nullable: false),
                    RobotMessage = table.Column<string>(type: "text", nullable: true),
                    EsppMessage = table.Column<string>(type: "text", nullable: true),
                    TaskStatusCode = table.Column<int>(type: "integer", nullable: false),
                    RobotConfigurationId = table.Column<Guid>(type: "uuid", nullable: false),
                    IdSeries = table.Column<Guid>(type: "uuid", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_RobotHistories", x => x.Id);
                    table.ForeignKey(
                        name: "FK_RobotHistories_RobotConfigurations_RobotConfigurationId",
                        column: x => x.RobotConfigurationId,
                        principalTable: "RobotConfigurations",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_RobotHistories_RobotHistoryLevels_HistoryLevel",
                        column: x => x.HistoryLevel,
                        principalTable: "RobotHistoryLevels",
                        principalColumn: "Level",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_RobotHistories_StatusTemplates_TaskStatusCode",
                        column: x => x.TaskStatusCode,
                        principalTable: "StatusTemplates",
                        principalColumn: "Code",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.InsertData(
                table: "Robots",
                columns: new[] { "Code", "Description", "Name" },
                values: new object[,]
                {
                    { 1, "Робот по созданию/изменению шаблона наряда ЕСПП", "TemplateOrder" },
                    { 2, "Робот по созданию/изменению расписания шаблона наряда в ЕСПП", "ScheduleOrder" }
                });

            migrationBuilder.CreateIndex(
                name: "IX_RobotConfigurations_RobotCode",
                table: "RobotConfigurations",
                column: "RobotCode");

            migrationBuilder.CreateIndex(
                name: "IX_RobotConfigurations_RobotStatusCode",
                table: "RobotConfigurations",
                column: "RobotStatusCode");

            migrationBuilder.CreateIndex(
                name: "IX_RobotConfigurations_TaskStatusCode",
                table: "RobotConfigurations",
                column: "TaskStatusCode");

            migrationBuilder.CreateIndex(
                name: "IX_RobotConfigurations_TemplateId_RobotCode",
                table: "RobotConfigurations",
                columns: new[] { "TemplateId", "RobotCode" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_RobotHistories_HistoryLevel",
                table: "RobotHistories",
                column: "HistoryLevel");

            migrationBuilder.CreateIndex(
                name: "IX_RobotHistories_RobotConfigurationId",
                table: "RobotHistories",
                column: "RobotConfigurationId");

            migrationBuilder.CreateIndex(
                name: "IX_RobotHistories_TaskStatusCode",
                table: "RobotHistories",
                column: "TaskStatusCode");

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

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

            migrationBuilder.DropTable(
                name: "RobotConfigurations");

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