using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

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

            migrationBuilder.DropTable(
                name: "RobotTemplateHistories");

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

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

            migrationBuilder.RenameColumn(
                name: "StatusCode",
                table: "Templates",
                newName: "TaskStatusCode");

            migrationBuilder.RenameIndex(
                name: "IX_Templates_StatusCode",
                table: "Templates",
                newName: "IX_Templates_TaskStatusCode");

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

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

            migrationBuilder.RenameColumn(
                name: "TaskStatusCode",
                table: "Templates",
                newName: "StatusCode");

            migrationBuilder.RenameIndex(
                name: "IX_Templates_TaskStatusCode",
                table: "Templates",
                newName: "IX_Templates_StatusCode");

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

            migrationBuilder.AddColumn<DateTimeOffset>(
                name: "RobotLastStatusUpdated",
                table: "Templates",
                type: "timestamp with time zone",
                nullable: true);

            migrationBuilder.CreateTable(
                name: "RobotTemplateHistories",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    HistoryLevel = table.Column<int>(type: "integer", nullable: false),
                    TemplateId = table.Column<Guid>(type: "uuid", nullable: false),
                    TemplateStatusCode = table.Column<int>(type: "integer", nullable: false),
                    DateCreated = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    EsppMessage = table.Column<string>(type: "text", nullable: true),
                    IdSeries = table.Column<Guid>(type: "uuid", nullable: false),
                    RobotMessage = table.Column<string>(type: "text", nullable: true)
                },
                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_TaskStatuses_TemplateStatusCode",
                        column: x => x.TemplateStatusCode,
                        principalTable: "TaskStatuses",
                        principalColumn: "Code",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_RobotTemplateHistories_Templates_TemplateId",
                        column: x => x.TemplateId,
                        principalTable: "Templates",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            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_TaskStatuses_StatusCode",
                table: "Templates",
                column: "StatusCode",
                principalTable: "TaskStatuses",
                principalColumn: "Code");
        }
    }
}
