-- =====================================================================================
-- Script: 99_MARS_835_verify_rollback.sql
-- Purpose: Verify all exported files have been deleted from HIST bucket (HIST-only strategy)
-- Author: Grzegorz Michalski
-- Created: 2025-12-17
-- Updated: 2026-02-24 (Changed to HIST-only verification)
-- MARS Issue: MARS-835
-- Verification: Confirm complete rollback (no CSDB files remaining in HIST)
-- =====================================================================================

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

PROMPT =====================================================================================
PROMPT MARS-835 Rollback Verification
PROMPT =====================================================================================
PROMPT Checking that all CSDB export files have been deleted
PROMPT =====================================================================================

DECLARE
    vHistBucketUri VARCHAR2(500);
    vCredentialName VARCHAR2(100);
    vHistFileCount NUMBER := 0;
    
    TYPE t_folder_list IS TABLE OF VARCHAR2(200);
    vHistFolders t_folder_list;
BEGIN
    -- Get bucket URI
    vHistBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('ARCHIVE');
    vCredentialName := CT_MRDS.ENV_MANAGER.gvCredentialName;
    
    DBMS_OUTPUT.PUT_LINE('ROLLBACK VERIFICATION TIME: ' || TO_CHAR(SYSTIMESTAMP, 'YYYY-MM-DD HH24:MI:SS.FF3'));
    DBMS_OUTPUT.PUT_LINE('HIST Bucket URI: ' || vHistBucketUri);
    DBMS_OUTPUT.PUT_LINE('');
    
    -- Initialize folder list (all 6 tables in HIST)
    -- Initialize folder list (all 6 tables in HIST)
    vHistFolders := t_folder_list(
        'ARCHIVE/CSDB/CSDB_DEBT/',
        'ARCHIVE/CSDB/CSDB_DEBT_DAILY/',
        'ARCHIVE/CSDB/CSDB_INSTR_RAT_FULL/',
        'ARCHIVE/CSDB/CSDB_INSTR_DESC_FULL/',
        'ARCHIVE/CSDB/CSDB_ISSUER_RAT_FULL/',
        'ARCHIVE/CSDB/CSDB_ISSUER_DESC_FULL/'
    );
    
    DBMS_OUTPUT.PUT_LINE('=====================================================================================');
    DBMS_OUTPUT.PUT_LINE('Checking HIST Bucket (should be empty)');
    DBMS_OUTPUT.PUT_LINE('=====================================================================================');
    
    -- Check HIST bucket
    FOR i IN 1..vHistFolders.COUNT LOOP
        DECLARE
            vCount NUMBER := 0;
        BEGIN
            DBMS_OUTPUT.PUT_LINE('');
            DBMS_OUTPUT.PUT_LINE('Folder: ' || vHistFolders(i));
            
            FOR rec IN (
                SELECT object_name
                FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
                    credential_name => vCredentialName,
                    location_uri => vHistBucketUri || vHistFolders(i)
                ))
                WHERE object_name LIKE '%.parquet'
                FETCH FIRST 10 ROWS ONLY
            ) LOOP
                vCount := vCount + 1;
                vHistFileCount := vHistFileCount + 1;
                IF vCount <= 5 THEN
                    DBMS_OUTPUT.PUT_LINE('  [FOUND] ' || rec.object_name);
                END IF;
            END LOOP;
            
            IF vCount = 0 THEN
                DBMS_OUTPUT.PUT_LINE('  [OK] No Parquet files found');
            ELSE
                DBMS_OUTPUT.PUT_LINE('  [INFO] Found ' || vCount || '+ file(s) (showing first 5) - may be pre-existing files from before installation');
            END IF;
        EXCEPTION
            WHEN OTHERS THEN
                IF SQLCODE = -20404 THEN
                    DBMS_OUTPUT.PUT_LINE('  [OK] Folder does not exist or is empty');
                ELSE
                    DBMS_OUTPUT.PUT_LINE('  [ERROR] ' || SQLERRM);
                END IF;
        END;
    END LOOP;
    
    DBMS_OUTPUT.PUT_LINE('');
    DBMS_OUTPUT.PUT_LINE('=====================================================================================');
    DBMS_OUTPUT.PUT_LINE('Rollback Verification Summary');
    DBMS_OUTPUT.PUT_LINE('=====================================================================================');
    DBMS_OUTPUT.PUT_LINE('HIST bucket files remaining: ' || vHistFileCount || '+');
    DBMS_OUTPUT.PUT_LINE('');
    DBMS_OUTPUT.PUT_LINE('');
    
    IF vHistFileCount = 0 THEN
        DBMS_OUTPUT.PUT_LINE('[PASSED] ROLLBACK VERIFICATION PASSED');
        DBMS_OUTPUT.PUT_LINE('  All CSDB export files have been deleted or were not created');
        DBMS_OUTPUT.PUT_LINE('  HIST bucket is clean and ready for re-export if needed');
    ELSE
        DBMS_OUTPUT.PUT_LINE('[INFO] ROLLBACK VERIFICATION COMPLETED');
        DBMS_OUTPUT.PUT_LINE('  Found ' || vHistFileCount || '+ file(s) remaining in HIST bucket');
        DBMS_OUTPUT.PUT_LINE('  NOTE: These may be pre-existing files from before installation.');
        DBMS_OUTPUT.PUT_LINE('  Rollback only deletes files created during this export operation.');
        DBMS_OUTPUT.PUT_LINE('  If needed, manually verify and clean up remaining files.');
    END IF;
    
    DBMS_OUTPUT.PUT_LINE('=====================================================================================');
    
EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('ERROR during rollback verification: ' || SQLERRM);
        RAISE;
END;
/

PROMPT
PROMPT Rollback verification completed
PROMPT
