-- =====================================================================
-- Script: 06_MARS_828_configure_release01_tables.sql
-- MARS Issue: MARS-828
-- Purpose: Configure COMPLETE archival parameters for Release 01 tables
-- Author: Grzegorz Michalski
-- Date: 2026-02-05
-- 
-- Description:
--   Configures ALL archival parameters for 25 tables:
--   - ARCHIVAL_STRATEGY and MINIMUM_AGE_MONTHS
--   - Archival triggers (FILES/ROWS/BYTES thresholds)
--   - Statistics expiration (HOURS_TO_EXPIRE_STATISTICS)
--
--   Configuration by group:
--   - 19 LM tables: MINIMUM_AGE_MONTHS=0 (current month only), 10 files OR 100K rows OR 1GB, 24h stats
--   - 2 CSDB DEBT: MINIMUM_AGE_MONTHS=0 (current month only), 5 files OR 50K rows OR 512MB, 48h stats
--   - 4 CSDB ratings: MINIMUM_AGE_MONTHS=0 (current month only), 10 files OR 20K rows OR 256MB, 72h stats
--
-- Dependencies:
--   - A_SOURCE_FILE_CONFIG table with all archival columns
--   - TRG_BI_A_SRC_FILE_CFG_ARCH_VAL trigger
-- =====================================================================

PROMPT =====================================================================
PROMPT MARS-828: Enhanced Configuration - Complete Archival Parameters
PROMPT =====================================================================
PROMPT
PROMPT This script configures COMPLETE archival parameters for 25 tables
PROMPT
PROMPT LM Tables (19):
PROMPT   - Strategy: MINIMUM_AGE_MONTHS = 0 (current month only)
PROMPT   - Triggers: 10 files OR 100,000 rows OR 1 GB
PROMPT   - Stats Expiration: 24 hours
PROMPT
PROMPT CSDB DEBT Tables (2):
PROMPT   - Strategy: MINIMUM_AGE_MONTHS = 0 (current month only)
PROMPT   - Triggers: 5 files OR 50,000 rows OR 512 MB
PROMPT   - Stats Expiration: 48 hours
PROMPT
PROMPT CSDB Rating/Description Tables (4):
PROMPT   - Strategy: MINIMUM_AGE_MONTHS = 0 (current month only)
PROMPT   - Triggers: 10 files OR 20,000 rows OR 256 MB
PROMPT   - Stats Expiration: 72 hours
PROMPT
PROMPT Current timestamp: 
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS CURRENT_TIME FROM DUAL;
PROMPT
PROMPT =====================================================================
PROMPT SECTION 1: LM Tables Configuration (MINIMUM_AGE_MONTHS = 0)
PROMPT =====================================================================
PROMPT   Thresholds: 10 files OR 100K rows OR 1GB
PROMPT   Stats expire: 24 hours
PROMPT =====================================================================

-- Update all 19 LM tables in a single statement
UPDATE CT_MRDS.A_SOURCE_FILE_CONFIG
SET ARCHIVAL_STRATEGY = 'MINIMUM_AGE_MONTHS',
    MINIMUM_AGE_MONTHS = 0,  -- 0 = current month only
    ODS_SCHEMA_NAME = 'ODS',
    ARCHIVE_THRESHOLD_FILES_COUNT = 10,
    ARCHIVE_THRESHOLD_ROWS_COUNT = 100000,
    ARCHIVE_THRESHOLD_BYTES_SUM = 1073741824,  -- 1 GB
    HOURS_TO_EXPIRE_STATISTICS = 24,
    IS_ARCHIVE_ENABLED = 'Y',  -- Enable archival for all LM tables
    IS_KEEP_IN_TRASH = 'N'     -- Delete files immediately after archival (no TRASH retention)
WHERE SOURCE_FILE_TYPE = 'INPUT'
  AND A_SOURCE_KEY = 'LM'
  AND TABLE_ID IN (
    'LM_STANDING_FACILITIES',
    'LM_STANDING_FACILITIES_HEADER',
    'LM_TTS_HEADER',
    'LM_TTS_ITEM',
    'LM_ADHOC_ADJUSTMENTS_HEADER',
    'LM_ADHOC_ADJUSTMENTS_ITEM',
    'LM_ADHOC_ADJUSTMENTS_ITEM_HEADER',
    'LM_BALANCESHEET_HEADER',
    'LM_BALANCESHEET_ITEM',
    'LM_CSM_ADJUSTMENTS_HEADER',
    'LM_CSM_ADJUSTMENTS_ITEM',
    'LM_CSM_ADJUSTMENTS_ITEM_HEADER',
    'LM_CURRENT_ACCOUNTS_HEADER',
    'LM_CURRENT_ACCOUNTS_ITEM',
    'LM_FORECAST_HEADER',
    'LM_FORECAST_ITEM',
    'LM_QRE_ADJUSTMENTS_HEADER',
    'LM_QRE_ADJUSTMENTS_ITEM',
    'LM_QRE_ADJUSTMENTS_ITEM_HEADER'
  );

PROMPT
PROMPT LM tables configuration completed
PROMPT

PROMPT =====================================================================
PROMPT SECTION 2: CSDB DEBT Tables (MINIMUM_AGE_MONTHS = 0)
PROMPT =====================================================================
PROMPT   Thresholds: 5 files OR 50K rows OR 512MB
PROMPT   Stats expire: 48 hours
PROMPT =====================================================================

-- Update CSDB DEBT tables (current month only)
UPDATE CT_MRDS.A_SOURCE_FILE_CONFIG
SET ARCHIVAL_STRATEGY = 'MINIMUM_AGE_MONTHS',
    MINIMUM_AGE_MONTHS = 0,
    ODS_SCHEMA_NAME = 'ODS',
    ARCHIVE_THRESHOLD_FILES_COUNT = 5,
    ARCHIVE_THRESHOLD_ROWS_COUNT = 50000,
    ARCHIVE_THRESHOLD_BYTES_SUM = 536870912,  -- 512 MB
    HOURS_TO_EXPIRE_STATISTICS = 48,
    IS_ARCHIVE_ENABLED = 'Y',  -- Enable archival for CSDB DEBT tables
    IS_KEEP_IN_TRASH = 'N'     -- Delete files immediately after archival (no TRASH retention)
WHERE SOURCE_FILE_TYPE = 'INPUT'
  AND A_SOURCE_KEY = 'CSDB'
  AND TABLE_ID IN ('CSDB_DEBT', 'CSDB_DEBT_DAILY');

PROMPT
PROMPT CSDB DEBT tables configuration completed
PROMPT

PROMPT =====================================================================
PROMPT SECTION 3: CSDB Rating/Description Tables (MINIMUM_AGE_MONTHS = 0)
PROMPT =====================================================================
PROMPT   Thresholds: 10 files OR 20K rows OR 256MB
PROMPT   Stats expire: 72 hours
PROMPT =====================================================================

-- Update CSDB rating/description tables
UPDATE CT_MRDS.A_SOURCE_FILE_CONFIG
SET ARCHIVAL_STRATEGY = 'MINIMUM_AGE_MONTHS',
    MINIMUM_AGE_MONTHS = 0,  -- 0 = current month only
    ODS_SCHEMA_NAME = 'ODS',
    ARCHIVE_THRESHOLD_FILES_COUNT = 10,
    ARCHIVE_THRESHOLD_ROWS_COUNT = 20000,
    ARCHIVE_THRESHOLD_BYTES_SUM = 268435456,  -- 256 MB
    HOURS_TO_EXPIRE_STATISTICS = 72,
    IS_ARCHIVE_ENABLED = 'Y',  -- Enable archival for CSDB rating/description tables
    IS_KEEP_IN_TRASH = 'N'     -- Delete files immediately after archival (no TRASH retention)
WHERE SOURCE_FILE_TYPE = 'INPUT'
  AND A_SOURCE_KEY = 'CSDB'
  AND TABLE_ID IN (
    'CSDB_INSTR_RAT_FULL',
    'CSDB_INSTR_DESC_FULL',
    'CSDB_ISSUER_RAT_FULL',
    'CSDB_ISSUER_DESC_FULL'
  );

PROMPT
PROMPT CSDB rating/description tables configuration completed
PROMPT

PROMPT =====================================================================
PROMPT Committing configuration changes...
PROMPT =====================================================================

COMMIT;

PROMPT
PROMPT Configuration committed successfully.
PROMPT

PROMPT =====================================================================
PROMPT VERIFICATION: Complete Archival Configuration
PROMPT =====================================================================

PROMPT
PROMPT LM Tables (MINIMUM_AGE_MONTHS = 0):
PROMPT

SELECT 
    TABLE_ID,
    ARCHIVAL_STRATEGY,
    MINIMUM_AGE_MONTHS,
    ARCHIVE_THRESHOLD_FILES_COUNT AS FILE_THR,
    ARCHIVE_THRESHOLD_ROWS_COUNT AS ROW_THR,
    ARCHIVE_THRESHOLD_BYTES_SUM AS BYTE_THR,
    HOURS_TO_EXPIRE_STATISTICS AS STATS_HRS,
    IS_ARCHIVE_ENABLED,
    IS_KEEP_IN_TRASH,
    CASE 
        WHEN ARCHIVAL_STRATEGY = 'MINIMUM_AGE_MONTHS' 
         AND MINIMUM_AGE_MONTHS = 0
         AND ARCHIVE_THRESHOLD_FILES_COUNT = 10
         AND ARCHIVE_THRESHOLD_ROWS_COUNT = 100000
         AND ARCHIVE_THRESHOLD_BYTES_SUM = 1073741824
         AND HOURS_TO_EXPIRE_STATISTICS = 24
         AND IS_ARCHIVE_ENABLED = 'Y'
         AND IS_KEEP_IN_TRASH = 'N'
        THEN 'OK'
        ELSE 'ERROR'
    END AS STATUS
FROM CT_MRDS.A_SOURCE_FILE_CONFIG
WHERE A_SOURCE_KEY = 'LM'
  AND SOURCE_FILE_TYPE = 'INPUT'
  AND TABLE_ID LIKE 'LM_%'
ORDER BY TABLE_ID;

PROMPT
PROMPT CSDB DEBT Tables (MINIMUM_AGE_MONTHS = 0):
PROMPT

SELECT 
    TABLE_ID,
    ARCHIVAL_STRATEGY,
    MINIMUM_AGE_MONTHS,
    ARCHIVE_THRESHOLD_FILES_COUNT AS FILE_THR,
    ARCHIVE_THRESHOLD_ROWS_COUNT AS ROW_THR,
    ARCHIVE_THRESHOLD_BYTES_SUM AS BYTE_THR,
    HOURS_TO_EXPIRE_STATISTICS AS STATS_HRS,
    IS_ARCHIVE_ENABLED,
    IS_KEEP_IN_TRASH,
    CASE 
        WHEN ARCHIVAL_STRATEGY = 'MINIMUM_AGE_MONTHS' 
         AND MINIMUM_AGE_MONTHS = 0
         AND ARCHIVE_THRESHOLD_FILES_COUNT = 5
         AND ARCHIVE_THRESHOLD_ROWS_COUNT = 50000
         AND ARCHIVE_THRESHOLD_BYTES_SUM = 536870912
         AND HOURS_TO_EXPIRE_STATISTICS = 48
         AND IS_ARCHIVE_ENABLED = 'Y'
         AND IS_KEEP_IN_TRASH = 'N'
        THEN 'OK'
        ELSE 'ERROR'
    END AS STATUS
FROM CT_MRDS.A_SOURCE_FILE_CONFIG
WHERE A_SOURCE_KEY = 'CSDB'
  AND SOURCE_FILE_TYPE = 'INPUT'
  AND TABLE_ID IN ('CSDB_DEBT', 'CSDB_DEBT_DAILY')
ORDER BY TABLE_ID;

PROMPT
PROMPT CSDB Rating/Description Tables (MINIMUM_AGE_MONTHS = 0):
PROMPT

SELECT 
    TABLE_ID,
    ARCHIVAL_STRATEGY,
    MINIMUM_AGE_MONTHS,
    ARCHIVE_THRESHOLD_FILES_COUNT AS FILE_THR,
    ARCHIVE_THRESHOLD_ROWS_COUNT AS ROW_THR,
    ARCHIVE_THRESHOLD_BYTES_SUM AS BYTE_THR,
    HOURS_TO_EXPIRE_STATISTICS AS STATS_HRS,
    IS_ARCHIVE_ENABLED,
    IS_KEEP_IN_TRASH,
    CASE 
        WHEN ARCHIVAL_STRATEGY = 'MINIMUM_AGE_MONTHS' 
         AND MINIMUM_AGE_MONTHS = 0
         AND ARCHIVE_THRESHOLD_FILES_COUNT = 10
         AND ARCHIVE_THRESHOLD_ROWS_COUNT = 20000
         AND ARCHIVE_THRESHOLD_BYTES_SUM = 268435456
         AND HOURS_TO_EXPIRE_STATISTICS = 72
         AND IS_ARCHIVE_ENABLED = 'Y'
         AND IS_KEEP_IN_TRASH = 'N'
        THEN 'OK'
        ELSE 'ERROR'
    END AS STATUS
FROM CT_MRDS.A_SOURCE_FILE_CONFIG
WHERE A_SOURCE_KEY = 'CSDB'
  AND SOURCE_FILE_TYPE = 'INPUT'
  AND TABLE_ID IN ('CSDB_INSTR_RAT_FULL', 'CSDB_INSTR_DESC_FULL', 'CSDB_ISSUER_RAT_FULL', 'CSDB_ISSUER_DESC_FULL')
ORDER BY TABLE_ID;

PROMPT
PROMPT =====================================================================
PROMPT Summary: Total Configured Tables with Full Parameters
PROMPT =====================================================================

SELECT 
    COUNT(*) AS TOTAL_CONFIGURED,
    SUM(CASE WHEN MINIMUM_AGE_MONTHS = 0 THEN 1 ELSE 0 END) AS CURRENT_MONTH_ONLY,
    SUM(CASE WHEN MINIMUM_AGE_MONTHS > 0 THEN 1 ELSE 0 END) AS MULTI_MONTH_RETENTION,
    SUM(CASE WHEN ARCHIVE_THRESHOLD_FILES_COUNT IS NOT NULL THEN 1 ELSE 0 END) AS WITH_FILE_THRESHOLD,
    SUM(CASE WHEN ARCHIVE_THRESHOLD_ROWS_COUNT IS NOT NULL THEN 1 ELSE 0 END) AS WITH_ROWS_THRESHOLD,
    SUM(CASE WHEN ARCHIVE_THRESHOLD_BYTES_SUM IS NOT NULL THEN 1 ELSE 0 END) AS WITH_BYTES_THRESHOLD,
    SUM(CASE WHEN HOURS_TO_EXPIRE_STATISTICS IS NOT NULL THEN 1 ELSE 0 END) AS WITH_STATS_EXPIRY,
    SUM(CASE WHEN IS_ARCHIVE_ENABLED = 'Y' THEN 1 ELSE 0 END) AS ARCHIVAL_ENABLED,
    SUM(CASE WHEN IS_KEEP_IN_TRASH = 'N' THEN 1 ELSE 0 END) AS IMMEDIATE_DELETE
FROM CT_MRDS.A_SOURCE_FILE_CONFIG
WHERE SOURCE_FILE_TYPE = 'INPUT'
  AND ((A_SOURCE_KEY = 'LM' AND TABLE_ID LIKE 'LM_%')
    OR (A_SOURCE_KEY = 'CSDB' AND TABLE_ID IN (
        'CSDB_DEBT', 'CSDB_DEBT_DAILY', 
        'CSDB_INSTR_RAT_FULL', 'CSDB_INSTR_DESC_FULL', 
        'CSDB_ISSUER_RAT_FULL', 'CSDB_ISSUER_DESC_FULL'
      )));

PROMPT
PROMPT Expected: 
PROMPT   - TOTAL_CONFIGURED = 25
PROMPT   - WITH_FILE_THRESHOLD = 25
PROMPT   - WITH_ROWS_THRESHOLD = 25
PROMPT   - WITH_BYTES_THRESHOLD = 25
PROMPT   - WITH_STATS_EXPIRY = 25
PROMPT   - ARCHIVAL_ENABLED = 25 (all tables enabled for archival)
PROMPT   - IMMEDIATE_DELETE = 25 (all tables delete files immediately, no TRASH retention)
PROMPT

PROMPT =====================================================================
PROMPT Parameter Configuration Summary by Group
PROMPT =====================================================================

SELECT 
    CASE 
        WHEN A_SOURCE_KEY = 'LM' THEN 'LM Tables'
        WHEN TABLE_ID LIKE 'CSDB_DEBT%' THEN 'CSDB DEBT'
        ELSE 'CSDB Ratings'
    END AS GROUP_NAME,
    COUNT(*) AS TABLE_COUNT,
    MAX(ARCHIVAL_STRATEGY) AS STRATEGY,
    MAX(MINIMUM_AGE_MONTHS) AS MIN_AGE,
    MAX(ARCHIVE_THRESHOLD_FILES_COUNT) AS FILES_THRESHOLD,
    MAX(ARCHIVE_THRESHOLD_ROWS_COUNT) AS ROWS_THRESHOLD,
    ROUND(MAX(ARCHIVE_THRESHOLD_BYTES_SUM)/1048576, 0) || ' MB' AS BYTES_THRESHOLD,
    MAX(HOURS_TO_EXPIRE_STATISTICS) AS STATS_HOURS
FROM CT_MRDS.A_SOURCE_FILE_CONFIG
WHERE SOURCE_FILE_TYPE = 'INPUT'
  AND ((A_SOURCE_KEY = 'LM' AND TABLE_ID LIKE 'LM_%')
    OR (A_SOURCE_KEY = 'CSDB' AND TABLE_ID IN (
        'CSDB_DEBT', 'CSDB_DEBT_DAILY', 
        'CSDB_INSTR_RAT_FULL', 'CSDB_INSTR_DESC_FULL', 
        'CSDB_ISSUER_RAT_FULL', 'CSDB_ISSUER_DESC_FULL'
      )))
GROUP BY 
    CASE 
        WHEN A_SOURCE_KEY = 'LM' THEN 'LM Tables'
        WHEN TABLE_ID LIKE 'CSDB_DEBT%' THEN 'CSDB DEBT'
        ELSE 'CSDB Ratings'
    END
ORDER BY GROUP_NAME;

PROMPT
PROMPT =====================================================================
PROMPT Configuration Complete
PROMPT =====================================================================
PROMPT
PROMPT Next Steps:
PROMPT 1. Review verification results above
PROMPT 2. All 25 tables should show STATUS = 'OK'
PROMPT 3. Verify parameter configuration summary matches expectations
PROMPT 4. Monitor archival process with new thresholds
PROMPT
PROMPT Archival Trigger Logic (OR condition):
PROMPT   - Archival starts when ANY threshold is exceeded
PROMPT   - Example: 10 files OR 100K rows OR 1GB triggers archival
PROMPT
PROMPT Statistics Refresh:
PROMPT   - LM: Every 24 hours (daily updates for frequent changes)
PROMPT   - CSDB DEBT: Every 48 hours (bi-daily for larger datasets)
PROMPT   - CSDB Ratings: Every 72 hours (every 3 days for stable data)
PROMPT
PROMPT Log file: 06_MARS_828_configure_release01_tables.log
PROMPT =====================================================================
