-- =====================================================================================
-- Script: 99_MARS_826_verify_rollback.sql
-- Purpose: Verify complete cleanup of MARS-826 exported files from HIST bucket
-- Author: Grzegorz Michalski
-- Created: 2025-12-02
-- Updated: 2025-12-15
-- 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 Rollback Verification: Complete Cleanup Check
PROMPT =====================================================================================

DECLARE
    vBucketUri VARCHAR2(500);
    vCredentialName VARCHAR2(100);
    vFileCount NUMBER := 0;
    vTotalFiles NUMBER := 0;
    vTablesWithFiles NUMBER := 0;
    vTablesClean NUMBER := 0;
    vRollbackSuccess BOOLEAN := TRUE;
    
    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('Bucket URI: ' || vBucketUri);
    DBMS_OUTPUT.PUT_LINE('Checking 19 MARS-826 export folders for remaining files...');
    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 remaining files
    FOR i IN 1..vFolders.COUNT LOOP
        vFileCount := 0;
        
        -- Count files in folder (using exception handling for missing folders)
        BEGIN
            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
                IF vFileCount = 0 THEN
                    DBMS_OUTPUT.PUT_LINE('ISSUE: ' || RPAD(vTables(i), 40) || ' - Files still exist:');
                    vRollbackSuccess := FALSE;
                    vTablesWithFiles := vTablesWithFiles + 1;
                END IF;
                
                vFileCount := vFileCount + 1;
                DBMS_OUTPUT.PUT_LINE('  [' || vFileCount || '] ' || rec.object_name || ' (' || ROUND(rec.bytes/1024, 1) || ' KB)');
            END LOOP;
        EXCEPTION
            WHEN OTHERS THEN
                -- Folder doesn't exist or access error - this is actually good for rollback
                NULL;
        END;
        
        IF vFileCount = 0 THEN
            DBMS_OUTPUT.PUT_LINE('OK:    ' || RPAD(vTables(i), 40) || ' - Clean (no files)');
            vTablesClean := vTablesClean + 1;
        ELSE
            vTotalFiles := vTotalFiles + vFileCount;
        END IF;
    END LOOP;
    
    DBMS_OUTPUT.PUT_LINE('');
    DBMS_OUTPUT.PUT_LINE('=====================================================================================');
    DBMS_OUTPUT.PUT_LINE('ROLLBACK VERIFICATION SUMMARY:');
    DBMS_OUTPUT.PUT_LINE('  Total tables checked: ' || vTables.COUNT);
    DBMS_OUTPUT.PUT_LINE('  Tables completely clean: ' || vTablesClean);
    DBMS_OUTPUT.PUT_LINE('  Tables with remaining files: ' || vTablesWithFiles);
    DBMS_OUTPUT.PUT_LINE('  Total remaining files: ' || vTotalFiles);
    DBMS_OUTPUT.PUT_LINE('');
    
    IF vRollbackSuccess THEN
        DBMS_OUTPUT.PUT_LINE('RESULT: ROLLBACK SUCCESSFUL');
        DBMS_OUTPUT.PUT_LINE('STATUS: All MARS-826 exported files have been completely removed');
        DBMS_OUTPUT.PUT_LINE('ACTION: No further action required - rollback is complete');
    ELSE
        DBMS_OUTPUT.PUT_LINE('RESULT: ROLLBACK INCOMPLETE');
        DBMS_OUTPUT.PUT_LINE('STATUS: ' || vTotalFiles || ' files remain in ' || vTablesWithFiles || ' folder(s)');
        DBMS_OUTPUT.PUT_LINE('ACTION: 1. Review rollback scripts for completeness');
        DBMS_OUTPUT.PUT_LINE('        2. Manual cleanup may be required for remaining files');
        DBMS_OUTPUT.PUT_LINE('        3. Check file permissions and bucket access');
    END IF;
    DBMS_OUTPUT.PUT_LINE('=====================================================================================');
    
EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('');
        DBMS_OUTPUT.PUT_LINE('VERIFICATION ERROR: ' || SQLERRM);
        RAISE;
END;
/
