-- Plan R S23 t8 (2026-05-15) — Destructive cleanup prod
-- Bro request 2026-05-15: "xóa hết các phiếu test cũ + quy trình cũ ko ghim cũng xóa hết"
-- Pre-flight Investigator audit: 28 PE active + 15 V2 unghim + 2 V1 inactive workflows
-- Backup: C:\Backup\SolutionErp_pre_cleanup_2026-05-15.bak (18.5MB verified)

USE SolutionErp;
GO

-- Filtered indexes require QUOTED_IDENTIFIER ON + ANSI_NULLS ON (Mig 29+ FilteredIndex)
SET QUOTED_IDENTIFIER ON;
SET ANSI_NULLS ON;
GO

PRINT '=== Plan R cleanup execute START ===';
PRINT 'Pre-cleanup state:';

SELECT 'PE active' AS Entity, COUNT(*) AS Cnt FROM PurchaseEvaluations WHERE IsDeleted = 0
UNION ALL SELECT 'PE soft-deleted', COUNT(*) FROM PurchaseEvaluations WHERE IsDeleted = 1
UNION ALL SELECT 'V2 workflow total', COUNT(*) FROM ApprovalWorkflows
UNION ALL SELECT 'V2 unghim (target)', COUNT(*) FROM ApprovalWorkflows WHERE IsUserSelectable = 0
UNION ALL SELECT 'V2 ghim (KEEP)', COUNT(*) FROM ApprovalWorkflows WHERE IsUserSelectable = 1
UNION ALL SELECT 'V1 workflow total', COUNT(*) FROM PurchaseEvaluationWorkflowDefinitions
UNION ALL SELECT 'V1 inactive (target)', COUNT(*) FROM PurchaseEvaluationWorkflowDefinitions WHERE IsActive = 0
UNION ALL SELECT 'V1 active (KEEP)', COUNT(*) FROM PurchaseEvaluationWorkflowDefinitions WHERE IsActive = 1;

PRINT '';
PRINT '=== Step 3: DELETE 28 PE active (cascade 446 child rows) ===';
BEGIN TRY
    BEGIN TRANSACTION DeletePE;
    DELETE FROM PurchaseEvaluations WHERE IsDeleted = 0;
    DECLARE @PEDeleted INT = @@ROWCOUNT;
    PRINT 'PE active rows deleted: ' + CAST(@PEDeleted AS NVARCHAR(10));

    -- Also clear soft-deleted ones for clean slate
    DELETE FROM PurchaseEvaluations WHERE IsDeleted = 1;
    DECLARE @PESoftDeleted INT = @@ROWCOUNT;
    PRINT 'PE soft-deleted rows deleted: ' + CAST(@PESoftDeleted AS NVARCHAR(10));

    COMMIT TRANSACTION DeletePE;
    PRINT 'Step 3 COMMIT OK';
END TRY
BEGIN CATCH
    ROLLBACK TRANSACTION DeletePE;
    PRINT 'Step 3 FAILED — rollback. Error: ' + ERROR_MESSAGE();
    THROW;
END CATCH;

PRINT '';
PRINT '=== Step 4: DELETE 15 V2 workflows unghim (cascade Steps + Levels) ===';
BEGIN TRY
    BEGIN TRANSACTION DeleteV2;
    DELETE FROM ApprovalWorkflows WHERE IsUserSelectable = 0;
    DECLARE @V2Deleted INT = @@ROWCOUNT;
    PRINT 'V2 workflows deleted: ' + CAST(@V2Deleted AS NVARCHAR(10));
    COMMIT TRANSACTION DeleteV2;
    PRINT 'Step 4 COMMIT OK';
END TRY
BEGIN CATCH
    ROLLBACK TRANSACTION DeleteV2;
    PRINT 'Step 4 FAILED — rollback. Error: ' + ERROR_MESSAGE();
    THROW;
END CATCH;

PRINT '';
PRINT '=== Step 5: DELETE 2 V1 workflows inactive (cascade Steps + Approvers) ===';
BEGIN TRY
    BEGIN TRANSACTION DeleteV1;
    DELETE FROM PurchaseEvaluationWorkflowDefinitions WHERE IsActive = 0;
    DECLARE @V1Deleted INT = @@ROWCOUNT;
    PRINT 'V1 workflows deleted: ' + CAST(@V1Deleted AS NVARCHAR(10));
    COMMIT TRANSACTION DeleteV1;
    PRINT 'Step 5 COMMIT OK';
END TRY
BEGIN CATCH
    ROLLBACK TRANSACTION DeleteV1;
    PRINT 'Step 5 FAILED — rollback. Error: ' + ERROR_MESSAGE();
    THROW;
END CATCH;

PRINT '';
PRINT '=== Post-cleanup verify ===';

SELECT 'PE total (expect 0)' AS Entity, COUNT(*) AS Cnt FROM PurchaseEvaluations
UNION ALL SELECT 'V2 workflow total (expect 2 ghim)', COUNT(*) FROM ApprovalWorkflows
UNION ALL SELECT 'V2 ghim remaining', COUNT(*) FROM ApprovalWorkflows WHERE IsUserSelectable = 1 AND IsActive = 1
UNION ALL SELECT 'V1 workflow total (expect 2 active)', COUNT(*) FROM PurchaseEvaluationWorkflowDefinitions
UNION ALL SELECT 'V1 active remaining', COUNT(*) FROM PurchaseEvaluationWorkflowDefinitions WHERE IsActive = 1;

PRINT '';
PRINT 'Remaining V2 workflows (ghim+active):';
SELECT Code, Version, ApplicableType, IsActive, IsUserSelectable, Name FROM ApprovalWorkflows ORDER BY Code, Version DESC;

PRINT '';
PRINT 'Remaining V1 workflows:';
SELECT Code, Version, EvaluationType, IsActive, Name FROM PurchaseEvaluationWorkflowDefinitions ORDER BY Code, Version DESC;

PRINT '';
PRINT '=== Plan R cleanup execute END ===';
