using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

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

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

            migrationBuilder.CreateTable(
                name: "WorkflowStepInnerSteps",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    WorkflowStepId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    Order = table.Column<int>(type: "int", nullable: false),
                    DepartmentId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    PositionLevel = table.Column<int>(type: "int", nullable: false),
                    Name = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: true),
                    SlaDays = table.Column<int>(type: "int", nullable: true),
                    IsRequired = table.Column<bool>(type: "bit", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    CreatedBy = table.Column<Guid>(type: "uniqueidentifier", 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_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_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);
        }

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

            migrationBuilder.DropTable(
                name: "WorkflowStepInnerSteps");

            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: "ContractDepartmentApprovals");

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