using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace SolutionErp.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class AddContractSigningPlans : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "ContractSigningPlans",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    MaKeHoach = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true),
                    PurchaseEvaluationId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    ProjectId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    DepartmentId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
                    DrafterUserId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    Phase = table.Column<int>(type: "int", nullable: false),
                    ApprovalWorkflowId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
                    CurrentWorkflowStepIndex = table.Column<int>(type: "int", nullable: true),
                    CurrentApprovalLevelOrder = table.Column<int>(type: "int", nullable: true),
                    SlaDeadline = table.Column<DateTime>(type: "datetime2", nullable: true),
                    HoSoLink = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: true),
                    GhiChu = table.Column<string>(type: "nvarchar(2000)", maxLength: 2000, nullable: true),
                    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_ContractSigningPlans", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ContractSigningPlans_ApprovalWorkflows_ApprovalWorkflowId",
                        column: x => x.ApprovalWorkflowId,
                        principalTable: "ApprovalWorkflows",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "ContractSigningPlanApprovals",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    ContractSigningPlanId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    FromPhase = table.Column<int>(type: "int", nullable: false),
                    ToPhase = table.Column<int>(type: "int", nullable: false),
                    Decision = table.Column<int>(type: "int", nullable: false),
                    Comment = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: true),
                    ApprovalWorkflowLevelId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
                    ApprovedByUserId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
                    ApprovedAt = table.Column<DateTime>(type: "datetime2", 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_ContractSigningPlanApprovals", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ContractSigningPlanApprovals_ContractSigningPlans_ContractSigningPlanId",
                        column: x => x.ContractSigningPlanId,
                        principalTable: "ContractSigningPlans",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "ContractSigningPlanAttachments",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    ContractSigningPlanId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    ContractSigningPlanDossierItemId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
                    FileName = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: false),
                    StoragePath = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: false),
                    FileSize = table.Column<long>(type: "bigint", nullable: false),
                    ContentType = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false),
                    Purpose = table.Column<int>(type: "int", nullable: false),
                    Note = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: true),
                    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_ContractSigningPlanAttachments", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ContractSigningPlanAttachments_ContractSigningPlans_ContractSigningPlanId",
                        column: x => x.ContractSigningPlanId,
                        principalTable: "ContractSigningPlans",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "ContractSigningPlanChangelogs",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    ContractSigningPlanId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    EntityType = table.Column<int>(type: "int", nullable: false),
                    EntityId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
                    Action = table.Column<int>(type: "int", nullable: false),
                    PhaseAtChange = table.Column<int>(type: "int", nullable: true),
                    UserId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
                    UserName = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: true),
                    Summary = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: true),
                    FieldChangesJson = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    ContextNote = table.Column<string>(type: "nvarchar(2000)", maxLength: 2000, nullable: true),
                    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_ContractSigningPlanChangelogs", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ContractSigningPlanChangelogs_ContractSigningPlans_ContractSigningPlanId",
                        column: x => x.ContractSigningPlanId,
                        principalTable: "ContractSigningPlans",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "ContractSigningPlanDossierItems",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    ContractSigningPlanId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    Kind = table.Column<int>(type: "int", nullable: false),
                    SupplierId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
                    Name = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: false),
                    Status = table.Column<int>(type: "int", nullable: false),
                    TvgsName = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: true),
                    TvgsResultAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    Note = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: true),
                    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_ContractSigningPlanDossierItems", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ContractSigningPlanDossierItems_ContractSigningPlans_ContractSigningPlanId",
                        column: x => x.ContractSigningPlanId,
                        principalTable: "ContractSigningPlans",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "ContractSigningPlanLevelOpinions",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    ContractSigningPlanId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    ApprovalWorkflowLevelId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    Comment = table.Column<string>(type: "nvarchar(2000)", maxLength: 2000, nullable: true),
                    SignedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
                    SignedByUserId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    SignedByFullName = table.Column<string>(type: "nvarchar(200)", maxLength: 200, 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_ContractSigningPlanLevelOpinions", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ContractSigningPlanLevelOpinions_ApprovalWorkflowLevels_ApprovalWorkflowLevelId",
                        column: x => x.ApprovalWorkflowLevelId,
                        principalTable: "ApprovalWorkflowLevels",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_ContractSigningPlanLevelOpinions_ContractSigningPlans_ContractSigningPlanId",
                        column: x => x.ContractSigningPlanId,
                        principalTable: "ContractSigningPlans",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "ContractSigningPlanLines",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    ContractSigningPlanId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    SupplierId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    PeReferenceAmount = table.Column<decimal>(type: "decimal(18,2)", precision: 18, scale: 2, nullable: false),
                    ProposedAmount = table.Column<decimal>(type: "decimal(18,2)", precision: 18, scale: 2, nullable: false),
                    ApprovedAmount = table.Column<decimal>(type: "decimal(18,2)", precision: 18, scale: 2, nullable: true),
                    ContractId = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
                    Note = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: true),
                    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_ContractSigningPlanLines", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ContractSigningPlanLines_ContractSigningPlans_ContractSigningPlanId",
                        column: x => x.ContractSigningPlanId,
                        principalTable: "ContractSigningPlans",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_ContractSigningPlanApprovals_ApprovalWorkflowLevelId",
                table: "ContractSigningPlanApprovals",
                column: "ApprovalWorkflowLevelId");

            migrationBuilder.CreateIndex(
                name: "IX_ContractSigningPlanApprovals_ContractSigningPlanId_ApprovedAt",
                table: "ContractSigningPlanApprovals",
                columns: new[] { "ContractSigningPlanId", "ApprovedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_ContractSigningPlanAttachments_ContractSigningPlanDossierItemId",
                table: "ContractSigningPlanAttachments",
                column: "ContractSigningPlanDossierItemId");

            migrationBuilder.CreateIndex(
                name: "IX_ContractSigningPlanAttachments_ContractSigningPlanId",
                table: "ContractSigningPlanAttachments",
                column: "ContractSigningPlanId");

            migrationBuilder.CreateIndex(
                name: "IX_ContractSigningPlanChangelogs_ContractSigningPlanId_CreatedAt",
                table: "ContractSigningPlanChangelogs",
                columns: new[] { "ContractSigningPlanId", "CreatedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_ContractSigningPlanChangelogs_ContractSigningPlanId_EntityType",
                table: "ContractSigningPlanChangelogs",
                columns: new[] { "ContractSigningPlanId", "EntityType" });

            migrationBuilder.CreateIndex(
                name: "IX_ContractSigningPlanDossierItems_ContractSigningPlanId",
                table: "ContractSigningPlanDossierItems",
                column: "ContractSigningPlanId");

            migrationBuilder.CreateIndex(
                name: "IX_ContractSigningPlanDossierItems_SupplierId",
                table: "ContractSigningPlanDossierItems",
                column: "SupplierId");

            migrationBuilder.CreateIndex(
                name: "IX_ContractSigningPlanLevelOpinions_ApprovalWorkflowLevelId",
                table: "ContractSigningPlanLevelOpinions",
                column: "ApprovalWorkflowLevelId");

            migrationBuilder.CreateIndex(
                name: "IX_ContractSigningPlanLevelOpinions_ContractSigningPlanId_ApprovalWorkflowLevelId",
                table: "ContractSigningPlanLevelOpinions",
                columns: new[] { "ContractSigningPlanId", "ApprovalWorkflowLevelId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_ContractSigningPlanLines_ContractId",
                table: "ContractSigningPlanLines",
                column: "ContractId");

            migrationBuilder.CreateIndex(
                name: "IX_ContractSigningPlanLines_ContractSigningPlanId_SupplierId",
                table: "ContractSigningPlanLines",
                columns: new[] { "ContractSigningPlanId", "SupplierId" },
                unique: true,
                filter: "[IsDeleted] = 0");

            migrationBuilder.CreateIndex(
                name: "IX_ContractSigningPlanLines_SupplierId",
                table: "ContractSigningPlanLines",
                column: "SupplierId");

            migrationBuilder.CreateIndex(
                name: "IX_ContractSigningPlans_ApprovalWorkflowId",
                table: "ContractSigningPlans",
                column: "ApprovalWorkflowId");

            migrationBuilder.CreateIndex(
                name: "IX_ContractSigningPlans_DepartmentId",
                table: "ContractSigningPlans",
                column: "DepartmentId");

            migrationBuilder.CreateIndex(
                name: "IX_ContractSigningPlans_DrafterUserId",
                table: "ContractSigningPlans",
                column: "DrafterUserId");

            migrationBuilder.CreateIndex(
                name: "IX_ContractSigningPlans_MaKeHoach",
                table: "ContractSigningPlans",
                column: "MaKeHoach",
                unique: true,
                filter: "[MaKeHoach] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_ContractSigningPlans_Phase_IsDeleted",
                table: "ContractSigningPlans",
                columns: new[] { "Phase", "IsDeleted" });

            migrationBuilder.CreateIndex(
                name: "IX_ContractSigningPlans_ProjectId",
                table: "ContractSigningPlans",
                column: "ProjectId");

            migrationBuilder.CreateIndex(
                name: "IX_ContractSigningPlans_PurchaseEvaluationId",
                table: "ContractSigningPlans",
                column: "PurchaseEvaluationId");
        }

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

            migrationBuilder.DropTable(
                name: "ContractSigningPlanAttachments");

            migrationBuilder.DropTable(
                name: "ContractSigningPlanChangelogs");

            migrationBuilder.DropTable(
                name: "ContractSigningPlanDossierItems");

            migrationBuilder.DropTable(
                name: "ContractSigningPlanLevelOpinions");

            migrationBuilder.DropTable(
                name: "ContractSigningPlanLines");

            migrationBuilder.DropTable(
                name: "ContractSigningPlans");
        }
    }
}
