using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace SolutionErp.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class AddTwoStageDeptApprovalAndSmartReject : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<bool>(
                name: "CanBypassReview",
                table: "Users",
                type: "bit",
                nullable: false,
                defaultValue: false);

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

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

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

            migrationBuilder.CreateTable(
                name: "BudgetDepartmentApprovals",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    BudgetId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    PhaseAtApproval = table.Column<int>(type: "int", nullable: false),
                    DepartmentId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    Stage = table.Column<int>(type: "int", nullable: false),
                    ApproverUserId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    ApproverRoleSnapshot = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: true),
                    Comment = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: true),
                    ApprovedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
                    IsBypassed = 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),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false),
                    DeletedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    DeletedBy = table.Column<Guid>(type: "uniqueidentifier", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_BudgetDepartmentApprovals", x => x.Id);
                    table.ForeignKey(
                        name: "FK_BudgetDepartmentApprovals_Budgets_BudgetId",
                        column: x => x.BudgetId,
                        principalTable: "Budgets",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "ContractDepartmentApprovals",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    ContractId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    PhaseAtApproval = table.Column<int>(type: "int", nullable: false),
                    DepartmentId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    Stage = table.Column<int>(type: "int", nullable: false),
                    ApproverUserId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    ApproverRoleSnapshot = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: true),
                    Comment = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: true),
                    ApprovedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
                    IsBypassed = 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),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false),
                    DeletedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    DeletedBy = table.Column<Guid>(type: "uniqueidentifier", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ContractDepartmentApprovals", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ContractDepartmentApprovals_Contracts_ContractId",
                        column: x => x.ContractId,
                        principalTable: "Contracts",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "PurchaseEvaluationDepartmentApprovals",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    PurchaseEvaluationId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    PhaseAtApproval = table.Column<int>(type: "int", nullable: false),
                    DepartmentId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    Stage = table.Column<int>(type: "int", nullable: false),
                    ApproverUserId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    ApproverRoleSnapshot = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: true),
                    Comment = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: true),
                    ApprovedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
                    IsBypassed = 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),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false),
                    DeletedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    DeletedBy = table.Column<Guid>(type: "uniqueidentifier", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_PurchaseEvaluationDepartmentApprovals", x => x.Id);
                    table.ForeignKey(
                        name: "FK_PurchaseEvaluationDepartmentApprovals_PurchaseEvaluations_PurchaseEvaluationId",
                        column: x => x.PurchaseEvaluationId,
                        principalTable: "PurchaseEvaluations",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_BudgetDepartmentApprovals_ApproverUserId",
                table: "BudgetDepartmentApprovals",
                column: "ApproverUserId");

            migrationBuilder.CreateIndex(
                name: "IX_BudgetDepartmentApprovals_BudgetId",
                table: "BudgetDepartmentApprovals",
                column: "BudgetId");

            migrationBuilder.CreateIndex(
                name: "IX_BudgetDepartmentApprovals_DepartmentId",
                table: "BudgetDepartmentApprovals",
                column: "DepartmentId");

            migrationBuilder.CreateIndex(
                name: "UX_BudgetDeptApprovals_Budget_Phase_Dept_Stage",
                table: "BudgetDepartmentApprovals",
                columns: new[] { "BudgetId", "PhaseAtApproval", "DepartmentId", "Stage" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_ContractDepartmentApprovals_ApproverUserId",
                table: "ContractDepartmentApprovals",
                column: "ApproverUserId");

            migrationBuilder.CreateIndex(
                name: "IX_ContractDepartmentApprovals_ContractId",
                table: "ContractDepartmentApprovals",
                column: "ContractId");

            migrationBuilder.CreateIndex(
                name: "IX_ContractDepartmentApprovals_DepartmentId",
                table: "ContractDepartmentApprovals",
                column: "DepartmentId");

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

            migrationBuilder.CreateIndex(
                name: "IX_PurchaseEvaluationDepartmentApprovals_ApproverUserId",
                table: "PurchaseEvaluationDepartmentApprovals",
                column: "ApproverUserId");

            migrationBuilder.CreateIndex(
                name: "IX_PurchaseEvaluationDepartmentApprovals_DepartmentId",
                table: "PurchaseEvaluationDepartmentApprovals",
                column: "DepartmentId");

            migrationBuilder.CreateIndex(
                name: "IX_PurchaseEvaluationDepartmentApprovals_PurchaseEvaluationId",
                table: "PurchaseEvaluationDepartmentApprovals",
                column: "PurchaseEvaluationId");

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

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

            migrationBuilder.DropTable(
                name: "ContractDepartmentApprovals");

            migrationBuilder.DropTable(
                name: "PurchaseEvaluationDepartmentApprovals");

            migrationBuilder.DropColumn(
                name: "CanBypassReview",
                table: "Users");

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

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

            migrationBuilder.DropColumn(
                name: "RejectedFromPhase",
                table: "Budgets");
        }
    }
}
