using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace SolutionErp.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class RefactorAdvancedOptionsToPerLevelAndDrafterUser : Migration
    {
        // Mig 29 (S21 t5) — Refactor Allow* options từ workflow-level (Mig 28
        // S21 t4) sang per-NV:
        //   - F1 (4 mode Trả lại) + F3 (Edit Section 2) = 5 flag move xuống
        //     ApprovalWorkflowLevels (per slot Approver).
        //   - F2 (AllowDrafterSkipToFinal) move xuống Users (per-Drafter user).
        //
        // Migration order: ADD columns mới TRƯỚC → BACKFILL bulk SQL từ workflow
        // → DROP columns workflow-level. Preserve admin config S21 t4 (Mig 28).
        //
        // EF auto-generated order (drop-then-add) đã được reorder manual.
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // ===== Stage 1: ADD 5 column trên ApprovalWorkflowLevels (per slot) =====
            migrationBuilder.AddColumn<bool>(
                name: "AllowReturnOneLevel",
                table: "ApprovalWorkflowLevels",
                type: "bit",
                nullable: false,
                defaultValue: false);

            migrationBuilder.AddColumn<bool>(
                name: "AllowReturnOneStep",
                table: "ApprovalWorkflowLevels",
                type: "bit",
                nullable: false,
                defaultValue: false);

            migrationBuilder.AddColumn<bool>(
                name: "AllowReturnToAssignee",
                table: "ApprovalWorkflowLevels",
                type: "bit",
                nullable: false,
                defaultValue: false);

            migrationBuilder.AddColumn<bool>(
                name: "AllowReturnToDrafter",
                table: "ApprovalWorkflowLevels",
                type: "bit",
                nullable: false,
                defaultValue: true);

            migrationBuilder.AddColumn<bool>(
                name: "AllowApproverEditDetails",
                table: "ApprovalWorkflowLevels",
                type: "bit",
                nullable: false,
                defaultValue: false);

            // ===== Stage 2: ADD 1 column trên Users (per-Drafter F2) =====
            migrationBuilder.AddColumn<bool>(
                name: "AllowDrafterSkipToFinal",
                table: "Users",
                type: "bit",
                nullable: false,
                defaultValue: false);

            // ===== Stage 3: BACKFILL bulk từ workflow-level (Mig 28) =====
            // Copy 5 F1+F3 flag từ workflow → all Levels của workflow đó.
            // SQL Server compatible (UPDATE ... FROM ... JOIN ...).
            migrationBuilder.Sql(@"
                UPDATE l SET
                    l.AllowReturnOneLevel = w.AllowReturnOneLevel,
                    l.AllowReturnOneStep = w.AllowReturnOneStep,
                    l.AllowReturnToAssignee = w.AllowReturnToAssignee,
                    l.AllowReturnToDrafter = w.AllowReturnToDrafter,
                    l.AllowApproverEditDetails = w.AllowApproverEditDetails
                FROM ApprovalWorkflowLevels l
                INNER JOIN ApprovalWorkflowSteps s ON s.Id = l.ApprovalWorkflowStepId
                INNER JOIN ApprovalWorkflows w ON w.Id = s.ApprovalWorkflowId;
            ");

            // Backfill Users.AllowDrafterSkipToFinal: set TRUE cho user nào
            // từng làm Drafter PE pin workflow có AllowDrafterSkipToFinal=true.
            // Conservative: preserve admin config Mig 28 cho user thực tế dùng,
            // các user khác giữ false (admin opt-in lần đầu).
            migrationBuilder.Sql(@"
                UPDATE u SET u.AllowDrafterSkipToFinal = 1
                FROM Users u
                WHERE EXISTS (
                    SELECT 1
                    FROM PurchaseEvaluations pe
                    INNER JOIN ApprovalWorkflows w ON w.Id = pe.ApprovalWorkflowId
                    WHERE pe.DrafterUserId = u.Id
                      AND w.AllowDrafterSkipToFinal = 1
                );
            ");

            // ===== Stage 4: DROP 6 column workflow-level (Mig 28 cleanup) =====
            migrationBuilder.DropColumn(
                name: "AllowApproverEditDetails",
                table: "ApprovalWorkflows");

            migrationBuilder.DropColumn(
                name: "AllowDrafterSkipToFinal",
                table: "ApprovalWorkflows");

            migrationBuilder.DropColumn(
                name: "AllowReturnOneLevel",
                table: "ApprovalWorkflows");

            migrationBuilder.DropColumn(
                name: "AllowReturnOneStep",
                table: "ApprovalWorkflows");

            migrationBuilder.DropColumn(
                name: "AllowReturnToAssignee",
                table: "ApprovalWorkflows");

            migrationBuilder.DropColumn(
                name: "AllowReturnToDrafter",
                table: "ApprovalWorkflows");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            // Rollback: re-add 6 column workflow-level + drop 5 Level + 1 User.
            // No reverse backfill (data loss accepted khi rollback).
            migrationBuilder.AddColumn<bool>(
                name: "AllowApproverEditDetails",
                table: "ApprovalWorkflows",
                type: "bit",
                nullable: false,
                defaultValue: false);

            migrationBuilder.AddColumn<bool>(
                name: "AllowDrafterSkipToFinal",
                table: "ApprovalWorkflows",
                type: "bit",
                nullable: false,
                defaultValue: false);

            migrationBuilder.AddColumn<bool>(
                name: "AllowReturnOneLevel",
                table: "ApprovalWorkflows",
                type: "bit",
                nullable: false,
                defaultValue: false);

            migrationBuilder.AddColumn<bool>(
                name: "AllowReturnOneStep",
                table: "ApprovalWorkflows",
                type: "bit",
                nullable: false,
                defaultValue: false);

            migrationBuilder.AddColumn<bool>(
                name: "AllowReturnToAssignee",
                table: "ApprovalWorkflows",
                type: "bit",
                nullable: false,
                defaultValue: false);

            migrationBuilder.AddColumn<bool>(
                name: "AllowReturnToDrafter",
                table: "ApprovalWorkflows",
                type: "bit",
                nullable: false,
                defaultValue: true);

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

            migrationBuilder.DropColumn(
                name: "AllowApproverEditDetails",
                table: "ApprovalWorkflowLevels");

            migrationBuilder.DropColumn(
                name: "AllowReturnOneLevel",
                table: "ApprovalWorkflowLevels");

            migrationBuilder.DropColumn(
                name: "AllowReturnOneStep",
                table: "ApprovalWorkflowLevels");

            migrationBuilder.DropColumn(
                name: "AllowReturnToAssignee",
                table: "ApprovalWorkflowLevels");

            migrationBuilder.DropColumn(
                name: "AllowReturnToDrafter",
                table: "ApprovalWorkflowLevels");
        }
    }
}
