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 TblsRobotHistoryAndTEmplates : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<int>(
                name: "RobotAttemptsNumber",
                table: "Templates",
                type: "integer",
                nullable: false,
                defaultValue: 0);

            migrationBuilder.AddColumn<DateTimeOffset>(
                name: "RobotLastStatusUpdated",
                table: "Templates",
                type: "timestamp with time zone",
                nullable: false,
                defaultValue: new DateTimeOffset(new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), new TimeSpan(0, 0, 0, 0, 0)));

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

            migrationBuilder.CreateTable(
                name: "RobotHistoryLevels",
                columns: table => new
                {
                    Level = 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_RobotHistoryLevels", x => x.Level);
                });

            migrationBuilder.CreateTable(
                name: "RobotStatuses",
                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_RobotStatuses", x => x.Code);
                });

            migrationBuilder.CreateTable(
                name: "RobotTemplateHistories",
                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),
                    TemplateId = table.Column<Guid>(type: "uuid", nullable: false),
                    TemplateStatusCode = table.Column<int>(type: "integer", nullable: false),
                    IdSeries = table.Column<Guid>(type: "uuid", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_RobotTemplateHistories", x => x.Id);
                    table.ForeignKey(
                        name: "FK_RobotTemplateHistories_RobotHistoryLevels_HistoryLevel",
                        column: x => x.HistoryLevel,
                        principalTable: "RobotHistoryLevels",
                        principalColumn: "Level",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_RobotTemplateHistories_StatusTemplates_TemplateStatusCode",
                        column: x => x.TemplateStatusCode,
                        principalTable: "StatusTemplates",
                        principalColumn: "Code",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_RobotTemplateHistories_Templates_TemplateId",
                        column: x => x.TemplateId,
                        principalTable: "Templates",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.InsertData(
                table: "RobotHistoryLevels",
                columns: new[] { "Level", "Description", "Name" },
                values: new object[,]
                {
                    { 1, "Робот начал работу ", "Start" },
                    { 5, "Информация", "Inforamtion" },
                    { 10, "Ошибка", "Error" },
                    { 15, "Успешно завершил работу", "Complete" },
                    { 20, "Завершил работу с ошибкой (не отработал до конца)", "Breake" }
                });

            migrationBuilder.InsertData(
                table: "RobotStatuses",
                columns: new[] { "Code", "Description", "Name" },
                values: new object[,]
                {
                    { 11, "Ожидание, ждет пока робот возьмет в работу.", "Wait" },
                    { 22, "Робот взял в работу.", "InProgress" },
                    { 33, "Ошибка отработки роботом. Требует ручного вмешательства.", "Error" },
                    { 44, "Робот успешно отработал.", "Complete" }
                });

            migrationBuilder.CreateIndex(
                name: "IX_Templates_RobotStatusCode",
                table: "Templates",
                column: "RobotStatusCode");

            migrationBuilder.CreateIndex(
                name: "IX_RobotTemplateHistories_HistoryLevel",
                table: "RobotTemplateHistories",
                column: "HistoryLevel");

            migrationBuilder.CreateIndex(
                name: "IX_RobotTemplateHistories_TemplateId",
                table: "RobotTemplateHistories",
                column: "TemplateId");

            migrationBuilder.CreateIndex(
                name: "IX_RobotTemplateHistories_TemplateStatusCode",
                table: "RobotTemplateHistories",
                column: "TemplateStatusCode");

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

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

            migrationBuilder.DropTable(
                name: "RobotStatuses");

            migrationBuilder.DropTable(
                name: "RobotTemplateHistories");

            migrationBuilder.DropTable(
                name: "RobotHistoryLevels");

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

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

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

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