using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace SolutionErp.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class RefactorWorkflowToFlatModel : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_ContractDepartmentApprovals_WorkflowStepInnerSteps_InnerStepId",
                table: "ContractDepartmentApprovals");

            migrationBuilder.DropForeignKey(
                name: "FK_PurchaseEvaluationDepartmentApprovals_PurchaseEvaluationWorkflowStepInnerSteps_InnerStepId",
                table: "PurchaseEvaluationDepartmentApprovals");

            migrationBuilder.DropTable(
                name: "PurchaseEvaluationWorkflowStepInnerSteps");

            migrationBuilder.DropTable(
                name: "WorkflowStepInnerSteps");

            migrationBuilder.DropIndex(
                name: "IX_PurchaseEvaluationDepartmentApprovals_InnerStepId",
                table: "PurchaseEvaluationDepartmentApprovals");

            migrationBuilder.DropIndex(
                name: "UX_PEDeptApprovals_PE_Phase_Dept_Stage",
                table: "PurchaseEvaluationDepartmentApprovals");

            migrationBuilder.DropIndex(
                name: "UX_PEDeptApprovals_PE_Phase_InnerStep",
                table: "PurchaseEvaluationDepartmentApprovals");

            migrationBuilder.DropIndex(
                name: "IX_ContractDepartmentApprovals_InnerStepId",
                table: "ContractDepartmentApprovals");

            migrationBuilder.DropIndex(
                name: "UX_ContractDeptApprovals_Contract_Phase_Dept_Stage",
                table: "ContractDepartmentApprovals");

            migrationBuilder.DropIndex(
                name: "UX_ContractDeptApprovals_Contract_Phase_InnerStep",
                table: "ContractDepartmentApprovals");

            migrationBuilder.DropColumn(
                name: "InnerStepId",
                table: "PurchaseEvaluationDepartmentApprovals");

            migrationBuilder.DropColumn(
                name: "InnerStepId",
                table: "ContractDepartmentApprovals");

            migrationBuilder.AddColumn<Guid>(
                name: "DepartmentId",
                table: "WorkflowSteps",
                type: "uniqueidentifier",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "PositionLevel",
                table: "WorkflowSteps",
                type: "int",
                nullable: true);

            migrationBuilder.AddColumn<Guid>(
                name: "DepartmentId",
                table: "PurchaseEvaluationWorkflowSteps",
                type: "uniqueidentifier",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "PositionLevel",
                table: "PurchaseEvaluationWorkflowSteps",
                type: "int",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "CurrentWorkflowStepIndex",
                table: "PurchaseEvaluations",
                type: "int",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "RejectedAtStepIndex",
                table: "PurchaseEvaluations",
                type: "int",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "CurrentWorkflowStepIndex",
                table: "Contracts",
                type: "int",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "RejectedAtStepIndex",
                table: "Contracts",
                type: "int",
                nullable: true);

            migrationBuilder.CreateIndex(
                name: "IX_WorkflowSteps_DepartmentId",
                table: "WorkflowSteps",
                column: "DepartmentId");

            migrationBuilder.CreateIndex(
                name: "IX_PurchaseEvaluationWorkflowSteps_DepartmentId",
                table: "PurchaseEvaluationWorkflowSteps",
                column: "DepartmentId");

            migrationBuilder.CreateIndex(
                name: "UX_PEDeptApprovals_PE_Phase_Dept_Stage",
                table: "PurchaseEvaluationDepartmentApprovals",
                columns: new[] { "PurchaseEvaluationId", "PhaseAtApproval", "DepartmentId", "Stage" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "UX_ContractDeptApprovals_Contract_Phase_Dept_Stage",
                table: "ContractDepartmentApprovals",
                columns: new[] { "ContractId", "PhaseAtApproval", "DepartmentId", "Stage" },
                unique: true);

            migrationBuilder.AddForeignKey(
                name: "FK_PurchaseEvaluationWorkflowSteps_Departments_DepartmentId",
                table: "PurchaseEvaluationWorkflowSteps",
                column: "DepartmentId",
                principalTable: "Departments",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);

            migrationBuilder.AddForeignKey(
                name: "FK_WorkflowSteps_Departments_DepartmentId",
                table: "WorkflowSteps",
                column: "DepartmentId",
                principalTable: "Departments",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        }

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

            migrationBuilder.DropForeignKey(
                name: "FK_WorkflowSteps_Departments_DepartmentId",
                table: "WorkflowSteps");

            migrationBuilder.DropIndex(
                name: "IX_WorkflowSteps_DepartmentId",
                table: "WorkflowSteps");

            migrationBuilder.DropIndex(
                name: "IX_PurchaseEvaluationWorkflowSteps_DepartmentId",
                table: "PurchaseEvaluationWorkflowSteps");

            migrationBuilder.DropIndex(
                name: "UX_PEDeptApprovals_PE_Phase_Dept_Stage",
                table: "PurchaseEvaluationDepartmentApprovals");

            migrationBuilder.DropIndex(
                name: "UX_ContractDeptApprovals_Contract_Phase_Dept_Stage",
                table: "ContractDepartmentApprovals");

            migrationBuilder.DropColumn(
                name: "DepartmentId",
                table: "WorkflowSteps");

            migrationBuilder.DropColumn(
                name: "PositionLevel",
                table: "WorkflowSteps");

            migrationBuilder.DropColumn(
                name: "DepartmentId",
                table: "PurchaseEvaluationWorkflowSteps");

            migrationBuilder.DropColumn(
                name: "PositionLevel",
                table: "PurchaseEvaluationWorkflowSteps");

            migrationBuilder.DropColumn(
                name: "CurrentWorkflowStepIndex",
                table: "PurchaseEvaluations");

            migrationBuilder.DropColumn(
                name: "RejectedAtStepIndex",
                table: "PurchaseEvaluations");

            migrationBuilder.DropColumn(
                name: "CurrentWorkflowStepIndex",
                table: "Contracts");

            migrationBuilder.DropColumn(
                name: "RejectedAtStepIndex",
                table: "Contracts");

            migrationBuilder.AddColumn<Guid>(
                name: "InnerStepId",
                table: "PurchaseEvaluationDepartmentApprovals",
                type: "uniqueidentifier",
                nullable: true);

            migrationBuilder.AddColumn<Guid>(
                name: "InnerStepId",
                table: "ContractDepartmentApprovals",
                type: "uniqueidentifier",
                nullable: true);

            migrationBuilder.CreateTable(
                name: "PurchaseEvaluationWorkflowStepInnerSteps",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    PurchaseEvaluationWorkflowStepId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
                    CreatedBy = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
                    DepartmentId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    IsRequired = table.Column<bool>(type: "bit", nullable: false),
                    Name = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: true),
                    Order = table.Column<int>(type: "int", nullable: false),
                    PositionLevel = table.Column<int>(type: "int", nullable: false),
                    SlaDays = table.Column<int>(type: "int", nullable: true),
                    UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    UpdatedBy = table.Column<Guid>(type: "uniqueidentifier", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_PurchaseEvaluationWorkflowStepInnerSteps", x => x.Id);
                    table.ForeignKey(
                        name: "FK_PurchaseEvaluationWorkflowStepInnerSteps_Departments_DepartmentId",
                        column: x => x.DepartmentId,
                        principalTable: "Departments",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_PurchaseEvaluationWorkflowStepInnerSteps_PurchaseEvaluationWorkflowSteps_PurchaseEvaluationWorkflowStepId",
                        column: x => x.PurchaseEvaluationWorkflowStepId,
                        principalTable: "PurchaseEvaluationWorkflowSteps",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "WorkflowStepInnerSteps",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    WorkflowStepId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
                    CreatedBy = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
                    DepartmentId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    IsRequired = table.Column<bool>(type: "bit", nullable: false),
                    Name = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: true),
                    Order = table.Column<int>(type: "int", nullable: false),
                    PositionLevel = table.Column<int>(type: "int", nullable: false),
                    SlaDays = table.Column<int>(type: "int", nullable: true),
                    UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    UpdatedBy = table.Column<Guid>(type: "uniqueidentifier", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_WorkflowStepInnerSteps", x => x.Id);
                    table.ForeignKey(
                        name: "FK_WorkflowStepInnerSteps_Departments_DepartmentId",
                        column: x => x.DepartmentId,
                        principalTable: "Departments",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_WorkflowStepInnerSteps_WorkflowSteps_WorkflowStepId",
                        column: x => x.WorkflowStepId,
                        principalTable: "WorkflowSteps",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_PurchaseEvaluationDepartmentApprovals_InnerStepId",
                table: "PurchaseEvaluationDepartmentApprovals",
                column: "InnerStepId");

            migrationBuilder.CreateIndex(
                name: "UX_PEDeptApprovals_PE_Phase_Dept_Stage",
                table: "PurchaseEvaluationDepartmentApprovals",
                columns: new[] { "PurchaseEvaluationId", "PhaseAtApproval", "DepartmentId", "Stage" },
                unique: true,
                filter: "[InnerStepId] IS NULL");

            migrationBuilder.CreateIndex(
                name: "UX_PEDeptApprovals_PE_Phase_InnerStep",
                table: "PurchaseEvaluationDepartmentApprovals",
                columns: new[] { "PurchaseEvaluationId", "PhaseAtApproval", "InnerStepId" },
                unique: true,
                filter: "[InnerStepId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_ContractDepartmentApprovals_InnerStepId",
                table: "ContractDepartmentApprovals",
                column: "InnerStepId");

            migrationBuilder.CreateIndex(
                name: "UX_ContractDeptApprovals_Contract_Phase_Dept_Stage",
                table: "ContractDepartmentApprovals",
                columns: new[] { "ContractId", "PhaseAtApproval", "DepartmentId", "Stage" },
                unique: true,
                filter: "[InnerStepId] IS NULL");

            migrationBuilder.CreateIndex(
                name: "UX_ContractDeptApprovals_Contract_Phase_InnerStep",
                table: "ContractDepartmentApprovals",
                columns: new[] { "ContractId", "PhaseAtApproval", "InnerStepId" },
                unique: true,
                filter: "[InnerStepId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_PurchaseEvaluationWorkflowStepInnerSteps_DepartmentId",
                table: "PurchaseEvaluationWorkflowStepInnerSteps",
                column: "DepartmentId");

            migrationBuilder.CreateIndex(
                name: "IX_PurchaseEvaluationWorkflowStepInnerSteps_PurchaseEvaluationWorkflowStepId_Order",
                table: "PurchaseEvaluationWorkflowStepInnerSteps",
                columns: new[] { "PurchaseEvaluationWorkflowStepId", "Order" });

            migrationBuilder.CreateIndex(
                name: "IX_WorkflowStepInnerSteps_DepartmentId",
                table: "WorkflowStepInnerSteps",
                column: "DepartmentId");

            migrationBuilder.CreateIndex(
                name: "IX_WorkflowStepInnerSteps_WorkflowStepId_Order",
                table: "WorkflowStepInnerSteps",
                columns: new[] { "WorkflowStepId", "Order" });

            migrationBuilder.AddForeignKey(
                name: "FK_ContractDepartmentApprovals_WorkflowStepInnerSteps_InnerStepId",
                table: "ContractDepartmentApprovals",
                column: "InnerStepId",
                principalTable: "WorkflowStepInnerSteps",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);

            migrationBuilder.AddForeignKey(
                name: "FK_PurchaseEvaluationDepartmentApprovals_PurchaseEvaluationWorkflowStepInnerSteps_InnerStepId",
                table: "PurchaseEvaluationDepartmentApprovals",
                column: "InnerStepId",
                principalTable: "PurchaseEvaluationWorkflowStepInnerSteps",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        }
    }
}
