-- ============================================================================
-- S59 (2026-06-11) — Wipe transactional TESTING data trước khi team vào testing
-- Chỉ đạo: anh Kiệt FDC (Zalo 14:28 "xoá các cái testing trước đi em").
-- Anh chốt (AskUserQuestion S59): wipe 3 cụm + reset bộ đếm mã về 0.
--
-- XÓA:  10 PurchaseEvaluations (A/031-040, cascade child) · 7 Contracts [DEMO]
--       (cascade) · 64 Notifications · 1 ApprovalWorkflow V2 cũ (inactive+unghim)
--       · PurchaseEvaluationCodeSequences + ContractCodeSequences (generator tự
--       INSERT LastSeq=1 khi thiếu row → phiếu/HĐ thật đầu tiên = .../001).
-- GIỮ:  Projects/WorkItems/Suppliers/Departments (master S55) · Users/Roles/
--       Permissions/MenuItems · ContractTemplates/Clauses · ApprovalWorkflows
--       GHIM (7 active) · WorkflowDefinitions V1 + PE V1 defs (policy, không
--       phải transactional) · HRM catalogs · EmployeeProfiles.
-- FK:   PE child ALL CASCADE trừ Quotes NO_ACTION (multi-path) — single DELETE
--       PurchaseEvaluations vẫn pass vì cascade Details→Quotes dọn trước
--       end-of-statement check (proven scripts/plan-r-cleanup.sql S23).
-- Resurrect-safe: SeedDemoContracts/PurchaseEvaluations nằm trong gate
--       DemoSeed:Disabled=true (DbInitializer.cs) → không seed lại sau restart.
-- Run:  sqlcmd -S .\SQLEXPRESS -d SolutionErp -i s59-wipe-testing-data.sql
-- ============================================================================
SET QUOTED_IDENTIFIER ON;
SET NOCOUNT ON;

PRINT '=== BEFORE ===';
SELECT 'PE' AS T, COUNT(*) AS C FROM PurchaseEvaluations
UNION ALL SELECT 'Contracts', COUNT(*) FROM Contracts
UNION ALL SELECT 'Notifications', COUNT(*) FROM Notifications
UNION ALL SELECT 'AwV2_total', COUNT(*) FROM ApprovalWorkflows
UNION ALL SELECT 'PeSeq', COUNT(*) FROM PurchaseEvaluationCodeSequences
UNION ALL SELECT 'CtSeq', COUNT(*) FROM ContractCodeSequences;

BEGIN TRANSACTION;

-- 1. Phiếu PE (cascade: Suppliers/Details/Quotes/Approvals/Changelogs/
--    Attachments/LevelOpinions/DepartmentOpinions)
DELETE FROM PurchaseEvaluations;

-- 2. Hợp đồng [DEMO] (cascade: Approvals + 7 bảng type-details + Comments/Attachments)
DELETE FROM Contracts;

-- 3. Thông báo
DELETE FROM Notifications;

-- 4. Workflow V2 cũ không dùng (QT-DN-V2-001 v1 inactive+unghim — PE pin đã gone
--    ở bước 1 nên FK Restrict released). 7 workflow GHIM active KHÔNG match filter.
DELETE FROM ApprovalWorkflows WHERE IsActive = 0 AND IsUserSelectable = 0;

-- 5. Reset bộ đếm mã (DELETE row → generator INSERT lại LastSeq=1)
DELETE FROM PurchaseEvaluationCodeSequences;
DELETE FROM ContractCodeSequences;

PRINT '=== AFTER (expect 0 các bảng transactional, KEEP_* giữ nguyên) ===';
SELECT 'PE' AS T, COUNT(*) AS C FROM PurchaseEvaluations
UNION ALL SELECT 'PeSuppliers', COUNT(*) FROM PurchaseEvaluationSuppliers
UNION ALL SELECT 'PeDetails', COUNT(*) FROM PurchaseEvaluationDetails
UNION ALL SELECT 'PeQuotes', COUNT(*) FROM PurchaseEvaluationQuotes
UNION ALL SELECT 'PeApprovals', COUNT(*) FROM PurchaseEvaluationApprovals
UNION ALL SELECT 'PeChangelogs', COUNT(*) FROM PurchaseEvaluationChangelogs
UNION ALL SELECT 'PeAttachments', COUNT(*) FROM PurchaseEvaluationAttachments
UNION ALL SELECT 'PeLevelOpinions', COUNT(*) FROM PurchaseEvaluationLevelOpinions
UNION ALL SELECT 'Contracts', COUNT(*) FROM Contracts
UNION ALL SELECT 'CtApprovals', COUNT(*) FROM ContractApprovals
UNION ALL SELECT 'Notifications', COUNT(*) FROM Notifications
UNION ALL SELECT 'AwV2_total', COUNT(*) FROM ApprovalWorkflows
UNION ALL SELECT 'AwV2_active_ghim', COUNT(*) FROM ApprovalWorkflows WHERE IsActive = 1
UNION ALL SELECT 'PeSeq', COUNT(*) FROM PurchaseEvaluationCodeSequences
UNION ALL SELECT 'CtSeq', COUNT(*) FROM ContractCodeSequences
UNION ALL SELECT 'KEEP_Projects', COUNT(*) FROM Projects
UNION ALL SELECT 'KEEP_WorkItems', COUNT(*) FROM WorkItems
UNION ALL SELECT 'KEEP_Suppliers', COUNT(*) FROM Suppliers
UNION ALL SELECT 'KEEP_Users', COUNT(*) FROM Users
UNION ALL SELECT 'KEEP_Templates', COUNT(*) FROM ContractTemplates
UNION ALL SELECT 'KEEP_WfDefV1', COUNT(*) FROM WorkflowDefinitions
UNION ALL SELECT 'KEEP_PeWfDefV1', COUNT(*) FROM PurchaseEvaluationWorkflowDefinitions;

COMMIT TRANSACTION;
PRINT 'S59 wipe COMMITTED.';
