-- =====================================================================================
-- Script: 09_MARS_826_verify_exports.sql
-- Purpose: Verify all exported Parquet files exist in HIST bucket
-- Author: Grzegorz Michalski
-- Created: 2025-12-02
-- MARS Issue: MARS-826
-- Target Location: mrds_hist_dev/ARCHIVE/LM/
-- =====================================================================================

SET SERVEROUTPUT ON SIZE UNLIMITED;
SET FEEDBACK ON;
SET VERIFY OFF;
SET LINESIZE 200;

PROMPT =====================================================================================
PROMPT MARS-826 Verification: Checking exported files in HIST bucket
PROMPT =====================================================================================

DECLARE
    vBucketUri VARCHAR2(500);
    vCredentialName VARCHAR2(100);
    vFileCount NUMBER := 0;
    vTotalFiles NUMBER := 0;
    vTotalSize NUMBER := 0;
    vFolderSize NUMBER := 0;
    
    TYPE t_folder_list IS TABLE OF VARCHAR2(200);
    vFolders t_folder_list;
    
    TYPE t_table_list IS TABLE OF VARCHAR2(100);
    vTables t_table_list;
BEGIN
    -- Get bucket URI and credential from FILE_MANAGER configuration
    vBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('ARCHIVE');
    vCredentialName := CT_MRDS.ENV_MANAGER.gvCredentialName;
    
    DBMS_OUTPUT.PUT_LINE('VERIFICATION TIME: ' || TO_CHAR(SYSTIMESTAMP, 'YYYY-MM-DD HH24:MI:SS.FF3'));
    DBMS_OUTPUT.PUT_LINE('');
    
    -- Initialize folder and table lists for all 19 tables
    vFolders := t_folder_list(
        'ARCHIVE/LM/LM_ADHOC_ADJUSTMENTS_HEADER/',
        'ARCHIVE/LM/LM_ADHOC_ADJUSTMENTS_ITEM/',
        'ARCHIVE/LM/LM_ADHOC_ADJUSTMENTS_ITEM_HEADER/',
        'ARCHIVE/LM/LM_BALANCESHEET_HEADER/',
        'ARCHIVE/LM/LM_BALANCESHEET_ITEM/',
        'ARCHIVE/LM/LM_CSM_ADJUSTMENTS_HEADER/',
        'ARCHIVE/LM/LM_CSM_ADJUSTMENTS_ITEM/',
        'ARCHIVE/LM/LM_CSM_ADJUSTMENTS_ITEM_HEADER/',
        'ARCHIVE/LM/LM_STANDING_FACILITIES/',
        'ARCHIVE/LM/LM_STANDING_FACILITIES_HEADER/',
        'ARCHIVE/LM/LM_CURRENT_ACCOUNTS_HEADER/',
        'ARCHIVE/LM/LM_CURRENT_ACCOUNTS_ITEM/',
        'ARCHIVE/LM/LM_FORECAST_HEADER/',
        'ARCHIVE/LM/LM_FORECAST_ITEM/',
        'ARCHIVE/LM/LM_QRE_ADJUSTMENTS_HEADER/',
        'ARCHIVE/LM/LM_QRE_ADJUSTMENTS_ITEM/',
        'ARCHIVE/LM/LM_QRE_ADJUSTMENTS_ITEM_HEADER/',
        'ARCHIVE/LM/LM_TTS_HEADER/',
        'ARCHIVE/LM/LM_TTS_ITEM/'
    );
    
    vTables := t_table_list(
        'ADHOC_ADJ_HEADER',
        'ADHOC_ADJ_ITEM',
        'ADHOC_ADJ_ITEM_HEADER',
        'BALANCESHEET_HEADER',
        'BALANCESHEET_ITEM',
        'CSM_ADJ_HEADER',
        'CSM_ADJ_ITEM',
        'CSM_ADJ_ITEM_HEADER',
        'STANDING_FACILITY',
        'STANDING_FACILITY_HEADER',
        'MRR_IND_CURRENT_ACCOUNT_HEADER',
        'MRR_IND_CURRENT_ACCOUNT_ITEM',
        'FORECAST_HEADER',
        'FORECAST_ITEM',
        'QR_ADJ_HEADER',
        'QR_ADJ_ITEM',
        'QR_ADJ_ITEM_HEADER',
        'TTS_HEADER',
        'TTS_ITEM'
    );
    
    -- Check each folder
    FOR i IN 1..vFolders.COUNT LOOP
        vFileCount := 0;
        vFolderSize := 0;
        
        -- List all Parquet files in the folder
        FOR rec IN (
            SELECT object_name, bytes
            FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
                credential_name => vCredentialName,
                location_uri => vBucketUri || vFolders(i)
            ))
            WHERE object_name LIKE '%.parquet'
            ORDER BY object_name
        ) LOOP
            vFileCount := vFileCount + 1;
            vFolderSize := vFolderSize + rec.bytes;
        END LOOP;
        
        IF vFileCount > 0 THEN
            DBMS_OUTPUT.PUT_LINE('[OK] ' || RPAD(vTables(i), 35) || ' : ' || 
                                LPAD(vFileCount, 5) || ' files, ' || 
                                LPAD(ROUND(vFolderSize/1024/1024, 2), 10) || ' MB');
            vTotalFiles := vTotalFiles + vFileCount;
            vTotalSize := vTotalSize + vFolderSize;
        ELSE
            DBMS_OUTPUT.PUT_LINE('[MISSING] ' || vTables(i) || ' - NO FILES FOUND!');
        END IF;
    END LOOP;
    
    DBMS_OUTPUT.PUT_LINE('');
    DBMS_OUTPUT.PUT_LINE('=====================================================================================');
    DBMS_OUTPUT.PUT_LINE('VERIFICATION SUMMARY:');
    DBMS_OUTPUT.PUT_LINE('  Total tables exported: ' || vTables.COUNT);
    DBMS_OUTPUT.PUT_LINE('  Total files created: ' || vTotalFiles);
    DBMS_OUTPUT.PUT_LINE('  Total export size: ' || ROUND(vTotalSize/1024/1024/1024, 2) || ' GB');
    
    IF vTotalFiles = 0 THEN
        DBMS_OUTPUT.PUT_LINE('');
        DBMS_OUTPUT.PUT_LINE('ERROR: No export files found! Export may have failed.');
        RAISE_APPLICATION_ERROR(-20001, 'Export verification failed - no files found');
    ELSE
        DBMS_OUTPUT.PUT_LINE('');
        DBMS_OUTPUT.PUT_LINE('STATUS: Export verification SUCCESSFUL');
    END IF;
    DBMS_OUTPUT.PUT_LINE('=====================================================================================');
    
EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('');
        DBMS_OUTPUT.PUT_LINE('VERIFICATION ERROR: ' || SQLERRM);
        RAISE;
END;
/
