--=============================================================================================================================
-- MARS-835 Manual Cleanup - Delete remaining files after rollback
--=============================================================================================================================

SET SERVEROUTPUT ON SIZE UNLIMITED
SET DEFINE OFF

DECLARE
    vDataBucketUri VARCHAR2(500);
    vArchiveBucketUri VARCHAR2(500);
    vCredentialName VARCHAR2(100);
    vFileCount NUMBER := 0;
BEGIN
    -- Get bucket URIs and credential
    vDataBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('DATA');
    vArchiveBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('ARCHIVE');
    vCredentialName := CT_MRDS.ENV_MANAGER.gvCredentialName;
    
    DBMS_OUTPUT.PUT_LINE('========================================================================');
    DBMS_OUTPUT.PUT_LINE('MARS-835 Manual Cleanup');
    DBMS_OUTPUT.PUT_LINE('========================================================================');
    
    -- Delete DEBT CSV files from DATA bucket
    DBMS_OUTPUT.PUT_LINE(CHR(10) || '1. Deleting DEBT CSV files from DATA bucket...');
    FOR rec IN (
        SELECT object_name
        FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
            credential_name => vCredentialName,
            location_uri => vDataBucketUri || 'ODS/CSDB/CSDB_DEBT/'
        ))
        WHERE object_name LIKE 'LEGACY_DEBT%'
    ) LOOP
        BEGIN
            DBMS_CLOUD.DELETE_OBJECT(
                credential_name => vCredentialName,
                object_uri => vDataBucketUri || 'ODS/CSDB/CSDB_DEBT/' || rec.object_name
            );
            DBMS_OUTPUT.PUT_LINE('  Deleted: ' || rec.object_name);
            vFileCount := vFileCount + 1;
        EXCEPTION
            WHEN OTHERS THEN
                DBMS_OUTPUT.PUT_LINE('  ERROR: ' || rec.object_name || ' - ' || SQLERRM);
        END;
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('Total deleted: ' || vFileCount);
    
    -- Delete DEBT_DAILY CSV files from DATA bucket
    DBMS_OUTPUT.PUT_LINE(CHR(10) || '2. Deleting DEBT_DAILY CSV files from DATA bucket...');
    vFileCount := 0;
    FOR rec IN (
        SELECT object_name
        FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
            credential_name => vCredentialName,
            location_uri => vDataBucketUri || 'ODS/CSDB/CSDB_DEBT_DAILY/'
        ))
        WHERE object_name LIKE 'LEGACY_DEBT_DAILY%'
    ) LOOP
        BEGIN
            DBMS_CLOUD.DELETE_OBJECT(
                credential_name => vCredentialName,
                object_uri => vDataBucketUri || 'ODS/CSDB/CSDB_DEBT_DAILY/' || rec.object_name
            );
            DBMS_OUTPUT.PUT_LINE('  Deleted: ' || rec.object_name);
            vFileCount := vFileCount + 1;
        EXCEPTION
            WHEN OTHERS THEN
                DBMS_OUTPUT.PUT_LINE('  ERROR: ' || rec.object_name || ' - ' || SQLERRM);
        END;
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('Total deleted: ' || vFileCount);
    
    -- Delete DEBT Parquet files from ARCHIVE bucket
    DBMS_OUTPUT.PUT_LINE(CHR(10) || '3. Deleting DEBT Parquet files from ARCHIVE bucket...');
    vFileCount := 0;
    FOR rec IN (
        SELECT object_name
        FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
            credential_name => vCredentialName,
            location_uri => vArchiveBucketUri || 'ARCHIVE/CSDB/CSDB_DEBT/'
        ))
        WHERE object_name NOT LIKE '%/'
    ) LOOP
        BEGIN
            DBMS_CLOUD.DELETE_OBJECT(
                credential_name => vCredentialName,
                object_uri => vArchiveBucketUri || 'ARCHIVE/CSDB/CSDB_DEBT/' || rec.object_name
            );
            DBMS_OUTPUT.PUT_LINE('  Deleted: ' || rec.object_name);
            vFileCount := vFileCount + 1;
        EXCEPTION
            WHEN OTHERS THEN
                DBMS_OUTPUT.PUT_LINE('  ERROR: ' || rec.object_name || ' - ' || SQLERRM);
        END;
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('Total deleted: ' || vFileCount);
    
    -- Delete DEBT_DAILY Parquet files from ARCHIVE bucket
    DBMS_OUTPUT.PUT_LINE(CHR(10) || '4. Deleting DEBT_DAILY Parquet files from ARCHIVE bucket...');
    vFileCount := 0;
    FOR rec IN (
        SELECT object_name
        FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
            credential_name => vCredentialName,
            location_uri => vArchiveBucketUri || 'ARCHIVE/CSDB/CSDB_DEBT_DAILY/'
        ))
        WHERE object_name NOT LIKE '%/'
    ) LOOP
        BEGIN
            DBMS_CLOUD.DELETE_OBJECT(
                credential_name => vCredentialName,
                object_uri => vArchiveBucketUri || 'ARCHIVE/CSDB/CSDB_DEBT_DAILY/' || rec.object_name
            );
            DBMS_OUTPUT.PUT_LINE('  Deleted: ' || rec.object_name);
            vFileCount := vFileCount + 1;
        EXCEPTION
            WHEN OTHERS THEN
                DBMS_OUTPUT.PUT_LINE('  ERROR: ' || rec.object_name || ' - ' || SQLERRM);
        END;
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('Total deleted: ' || vFileCount);
    
    DBMS_OUTPUT.PUT_LINE(CHR(10) || '========================================================================');
    DBMS_OUTPUT.PUT_LINE('Manual cleanup completed');
    DBMS_OUTPUT.PUT_LINE('========================================================================');
END;
/
