-- ===================================================================
-- MARS-828: Fix CSDB configuration and test all archival strategies
-- ===================================================================
-- Purpose: Correct CSDB strategy and comprehensive testing
-- Author: Grzegorz Michalski
-- Date: 2026-01-28
-- ===================================================================

SET SERVEROUTPUT ON SIZE UNLIMITED
SET LINESIZE 200
SET PAGESIZE 1000

PROMPT
PROMPT ========================================
PROMPT MARS-828: Fix Configuration & Test Strategies
PROMPT ========================================
PROMPT

-- Step 1: Fix CSDB configurations (all CSDB_DEBT* tables)
PROMPT Step 1: Updating CSDB configurations...
PROMPT ========================================
UPDATE CT_MRDS.A_SOURCE_FILE_CONFIG
SET ARCHIVAL_STRATEGY = 'MINIMUM_AGE_MONTHS',
    MINIMUM_AGE_MONTHS = 6
WHERE A_SOURCE_KEY = 'CSDB'
  AND SOURCE_FILE_TYPE = 'INPUT'
  AND TABLE_ID LIKE 'CSDB_DEBT%';

COMMIT;

-- Step 2: Verify updated CSDB configurations
PROMPT
PROMPT Step 2: Verify CSDB Configurations
PROMPT ====================================
SELECT 
    SOURCE_FILE_ID,
    TABLE_ID,
    ARCHIVAL_STRATEGY,
    MINIMUM_AGE_MONTHS
FROM CT_MRDS.A_SOURCE_FILE_CONFIG
WHERE A_SOURCE_KEY = 'CSDB'
  AND SOURCE_FILE_TYPE = 'INPUT'
  AND TABLE_ID LIKE 'CSDB_DEBT%'
ORDER BY TABLE_ID;

-- Step 3: View final test configurations
PROMPT
PROMPT Step 3: Final Test Configurations
PROMPT ==================================
SELECT 
    A_SOURCE_KEY,
    SUBSTR(TABLE_ID, 1, 35) AS TABLE_ID,
    ARCHIVAL_STRATEGY,
    MINIMUM_AGE_MONTHS,
    DAYS_FOR_ARCHIVE_THRESHOLD
FROM CT_MRDS.A_SOURCE_FILE_CONFIG
WHERE SOURCE_FILE_TYPE = 'INPUT'
  AND (
      (A_SOURCE_KEY = 'C2D' AND SOURCE_FILE_ID = 'UC_ACC')
      OR (A_SOURCE_KEY = 'LM' AND SOURCE_FILE_ID = 'AdhocAdjustment' AND TABLE_ID = 'LM_ADHOC_ADJUSTMENTS_ITEM')
      OR (A_SOURCE_KEY = 'CSDB' AND TABLE_ID LIKE 'CSDB_DEBT%')
      OR (A_SOURCE_KEY = 'TOP' AND TABLE_ID = 'TOP_FULLBID_ARRAY_COMPILED')
  )
ORDER BY 
    CASE A_SOURCE_KEY
        WHEN 'C2D' THEN 1
        WHEN 'LM' THEN 2
        WHEN 'CSDB' THEN 3
        WHEN 'TOP' THEN 4
    END,
    TABLE_ID;

-- Step 4: Test GET_ARCHIVAL_WHERE_CLAUSE via DBMS_OUTPUT
PROMPT
PROMPT Step 4: Testing WHERE Clause Generation
PROMPT ========================================
DECLARE
    vConfig CT_MRDS.A_SOURCE_FILE_CONFIG%ROWTYPE;
    vWhereClause VARCHAR2(4000);
BEGIN
    -- Test 1: THRESHOLD_BASED
    SELECT * INTO vConfig 
    FROM CT_MRDS.A_SOURCE_FILE_CONFIG 
    WHERE A_SOURCE_KEY = 'C2D' 
      AND SOURCE_FILE_ID = 'UC_ACC' 
      AND ROWNUM = 1;
    DBMS_OUTPUT.PUT_LINE('========================================');
    DBMS_OUTPUT.PUT_LINE('Test 1: THRESHOLD_BASED (C2D/UC_ACC)');
    DBMS_OUTPUT.PUT_LINE('Expected: extract(day from (systimestamp - workflow_start)) > 30');
    DBMS_OUTPUT.PUT_LINE('NOTE: Cannot test private function directly');
    DBMS_OUTPUT.PUT_LINE('Strategy: ' || vConfig.ARCHIVAL_STRATEGY);
    DBMS_OUTPUT.PUT_LINE('Threshold: ' || vConfig.DAYS_FOR_ARCHIVE_THRESHOLD);
    
    -- Test 2: CURRENT_MONTH_ONLY
    SELECT * INTO vConfig 
    FROM CT_MRDS.A_SOURCE_FILE_CONFIG 
    WHERE A_SOURCE_KEY = 'LM' 
      AND SOURCE_FILE_ID = 'AdhocAdjustment'
      AND TABLE_ID = 'LM_ADHOC_ADJUSTMENTS_ITEM'
      AND ROWNUM = 1;
    DBMS_OUTPUT.PUT_LINE('========================================');
    DBMS_OUTPUT.PUT_LINE('Test 2: CURRENT_MONTH_ONLY (LM/AdhocAdjustment)');
    DBMS_OUTPUT.PUT_LINE('Expected: TRUNC(workflow_start, ''MM'') < TRUNC(SYSDATE, ''MM'')');
    DBMS_OUTPUT.PUT_LINE('Strategy: ' || vConfig.ARCHIVAL_STRATEGY);
    DBMS_OUTPUT.PUT_LINE('Min Age: ' || NVL(TO_CHAR(vConfig.MINIMUM_AGE_MONTHS), 'NULL'));
    
    -- Test 3: MINIMUM_AGE_MONTHS
    SELECT * INTO vConfig 
    FROM CT_MRDS.A_SOURCE_FILE_CONFIG 
    WHERE A_SOURCE_KEY = 'CSDB' 
      AND TABLE_ID = 'CSDB_DEBT'
      AND ROWNUM = 1;
    DBMS_OUTPUT.PUT_LINE('========================================');
    DBMS_OUTPUT.PUT_LINE('Test 3: MINIMUM_AGE_MONTHS (CSDB/CSDB_DEBT)');
    DBMS_OUTPUT.PUT_LINE('Expected: workflow_start < ADD_MONTHS(TRUNC(SYSDATE, ''MM''), -6)');
    DBMS_OUTPUT.PUT_LINE('Strategy: ' || vConfig.ARCHIVAL_STRATEGY);
    DBMS_OUTPUT.PUT_LINE('Min Age: ' || vConfig.MINIMUM_AGE_MONTHS || ' months');
    
    -- Test 4: HYBRID
    SELECT * INTO vConfig 
    FROM CT_MRDS.A_SOURCE_FILE_CONFIG 
    WHERE A_SOURCE_KEY = 'TOP' 
      AND TABLE_ID = 'TOP_FULLBID_ARRAY_COMPILED'
      AND ROWNUM = 1;
    DBMS_OUTPUT.PUT_LINE('========================================');
    DBMS_OUTPUT.PUT_LINE('Test 4: HYBRID (TOP/TOP_FULLBID_ARRAY_COMPILED)');
    DBMS_OUTPUT.PUT_LINE('Expected: TRUNC(workflow_start, ''MM'') < TRUNC(SYSDATE, ''MM'')');
    DBMS_OUTPUT.PUT_LINE('      AND workflow_start < ADD_MONTHS(TRUNC(SYSDATE, ''MM''), -3)');
    DBMS_OUTPUT.PUT_LINE('Strategy: ' || vConfig.ARCHIVAL_STRATEGY);
    DBMS_OUTPUT.PUT_LINE('Min Age: ' || vConfig.MINIMUM_AGE_MONTHS || ' months');
    DBMS_OUTPUT.PUT_LINE('========================================');
END;
/

-- Step 5: Test archival statistics simulation
PROMPT
PROMPT Step 5: Archive Statistics Simulation
PROMPT ======================================
PROMPT NOTE: This simulates what GATHER_TABLE_STAT would calculate
PROMPT

-- Create test scenario date ranges
DECLARE
    vCurrentDate DATE := TRUNC(SYSDATE);
    vCurrentMonth DATE := TRUNC(SYSDATE, 'MM');
    v3MonthsAgo DATE := ADD_MONTHS(TRUNC(SYSDATE, 'MM'), -3);
    v6MonthsAgo DATE := ADD_MONTHS(TRUNC(SYSDATE, 'MM'), -6);
    v30DaysAgo DATE := SYSDATE - 30;
BEGIN
    DBMS_OUTPUT.PUT_LINE('Current Date: ' || TO_CHAR(vCurrentDate, 'YYYY-MM-DD'));
    DBMS_OUTPUT.PUT_LINE('Current Month Start: ' || TO_CHAR(vCurrentMonth, 'YYYY-MM-DD'));
    DBMS_OUTPUT.PUT_LINE('3 Months Ago: ' || TO_CHAR(v3MonthsAgo, 'YYYY-MM-DD'));
    DBMS_OUTPUT.PUT_LINE('6 Months Ago: ' || TO_CHAR(v6MonthsAgo, 'YYYY-MM-DD'));
    DBMS_OUTPUT.PUT_LINE('30 Days Ago: ' || TO_CHAR(v30DaysAgo, 'YYYY-MM-DD HH24:MI:SS'));
    DBMS_OUTPUT.PUT_LINE('');
    
    -- Example: Data from 2024-08-15 (CSDB DEBT)
    -- Would be archived with MINIMUM_AGE_MONTHS=6?
    IF DATE '2024-08-15' < v6MonthsAgo THEN
        DBMS_OUTPUT.PUT_LINE('CSDB Example: 2024-08-15 WOULD be archived (older than 6 months)');
    ELSE
        DBMS_OUTPUT.PUT_LINE('CSDB Example: 2024-08-15 would NOT be archived (within 6 months)');
    END IF;
    
    -- Example: Data from 2025-12-20 (LM)
    -- Would be archived with CURRENT_MONTH_ONLY?
    IF TRUNC(DATE '2025-12-20', 'MM') < vCurrentMonth THEN
        DBMS_OUTPUT.PUT_LINE('LM Example: 2025-12-20 WOULD be archived (previous month)');
    ELSE
        DBMS_OUTPUT.PUT_LINE('LM Example: 2025-12-20 would NOT be archived (current month)');
    END IF;
    
    -- Example: Data from 2025-10-10 (TOP HYBRID)
    -- Would be archived with HYBRID (current month + 3 months)?
    IF TRUNC(DATE '2025-10-10', 'MM') < vCurrentMonth 
       AND DATE '2025-10-10' < v3MonthsAgo THEN
        DBMS_OUTPUT.PUT_LINE('TOP Example: 2025-10-10 WOULD be archived (previous month AND older than 3 months)');
    ELSE
        DBMS_OUTPUT.PUT_LINE('TOP Example: 2025-10-10 would NOT be archived (within 3 months or current month)');
    END IF;
END;
/

PROMPT
PROMPT ========================================
PROMPT MARS-828: Configuration & Testing Complete
PROMPT ========================================
PROMPT
PROMPT Summary:
PROMPT - All 4 strategies configured correctly
PROMPT - CSDB: 4 tables updated to MINIMUM_AGE_MONTHS (6 months)
PROMPT - LM: 1 table configured as CURRENT_MONTH_ONLY
PROMPT - TOP: 1 table configured as HYBRID (3 months)
PROMPT - C2D: 1 table kept as THRESHOLD_BASED (30 days)
PROMPT
PROMPT Next Steps:
PROMPT - Test ARCHIVE_TABLE_DATA with real data
PROMPT - Test GATHER_TABLE_STAT calculations
PROMPT - Verify archival operations in production scenario
PROMPT ========================================
