-- MARS-835: Manual cleanup of Parquet files only (after bugfix)
-- Description: Removes orphaned Parquet files from ARCHIVE bucket
-- Usage: Execute as CT_MRDS user

SET SERVEROUTPUT ON SIZE UNLIMITED

DECLARE
    vCredentialName VARCHAR2(100) := CT_MRDS.ENV_MANAGER.gvCredentialName;
    vHistBucketUri VARCHAR2(200) := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('ARCHIVE');
    vFileCount NUMBER := 0;
    vStartTime TIMESTAMP := SYSTIMESTAMP;
    vElapsedTime INTERVAL DAY TO SECOND;
BEGIN
    DBMS_OUTPUT.PUT_LINE('==========================================================');
    DBMS_OUTPUT.PUT_LINE('MANUAL CLEANUP: Parquet files only');
    DBMS_OUTPUT.PUT_LINE('==========================================================');
    DBMS_OUTPUT.PUT_LINE('Start Time: ' || TO_CHAR(vStartTime, 'YYYY-MM-DD HH24:MI:SS'));
    DBMS_OUTPUT.PUT_LINE('Credential: ' || vCredentialName);
    DBMS_OUTPUT.PUT_LINE('Archive Bucket: ' || vHistBucketUri);
    DBMS_OUTPUT.PUT_LINE('----------------------------------------------------------');
    
    -- Delete CSDB_DEBT Parquet files
    DBMS_OUTPUT.PUT_LINE(chr(10) || 'Deleting CSDB_DEBT Parquet files...');
    vFileCount := 0;
    
    FOR rec IN (
        SELECT object_name
        FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
            credential_name => vCredentialName,
            location_uri => vHistBucketUri || 'ARCHIVE/CSDB/CSDB_DEBT/'
        ))
        WHERE object_name NOT LIKE '%/'  -- Exclude directories
    ) LOOP
        BEGIN
            DBMS_CLOUD.DELETE_OBJECT(
                credential_name => vCredentialName,
                object_uri => vHistBucketUri || 'ARCHIVE/CSDB/CSDB_DEBT/' || rec.object_name
            );
            DBMS_OUTPUT.PUT_LINE('  Deleted: ' || rec.object_name);
            vFileCount := vFileCount + 1;
        EXCEPTION
            WHEN OTHERS THEN
                IF SQLCODE = -20404 THEN
                    DBMS_OUTPUT.PUT_LINE('  Skipped (not found): ' || rec.object_name);
                ELSE
                    DBMS_OUTPUT.PUT_LINE('  ERROR: ' || SQLERRM || ' - ' || rec.object_name);
                END IF;
        END;
    END LOOP;
    
    DBMS_OUTPUT.PUT_LINE('CSDB_DEBT Parquet files deleted: ' || vFileCount);
    
    -- Delete CSDB_DEBT_DAILY Parquet files
    DBMS_OUTPUT.PUT_LINE(chr(10) || 'Deleting CSDB_DEBT_DAILY Parquet files...');
    vFileCount := 0;
    
    FOR rec IN (
        SELECT object_name
        FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
            credential_name => vCredentialName,
            location_uri => vHistBucketUri || 'ARCHIVE/CSDB/CSDB_DEBT_DAILY/'
        ))
        WHERE object_name NOT LIKE '%/'  -- Exclude directories
    ) LOOP
        BEGIN
            DBMS_CLOUD.DELETE_OBJECT(
                credential_name => vCredentialName,
                object_uri => vHistBucketUri || 'ARCHIVE/CSDB/CSDB_DEBT_DAILY/' || rec.object_name
            );
            DBMS_OUTPUT.PUT_LINE('  Deleted: ' || rec.object_name);
            vFileCount := vFileCount + 1;
        EXCEPTION
            WHEN OTHERS THEN
                IF SQLCODE = -20404 THEN
                    DBMS_OUTPUT.PUT_LINE('  Skipped (not found): ' || rec.object_name);
                ELSE
                    DBMS_OUTPUT.PUT_LINE('  ERROR: ' || SQLERRM || ' - ' || rec.object_name);
                END IF;
        END;
    END LOOP;
    
    DBMS_OUTPUT.PUT_LINE('CSDB_DEBT_DAILY Parquet files deleted: ' || vFileCount);
    
    -- Final summary
    vElapsedTime := SYSTIMESTAMP - vStartTime;
    DBMS_OUTPUT.PUT_LINE('----------------------------------------------------------');
    DBMS_OUTPUT.PUT_LINE('End Time: ' || TO_CHAR(SYSTIMESTAMP, 'YYYY-MM-DD HH24:MI:SS'));
    DBMS_OUTPUT.PUT_LINE('Elapsed Time: ' || vElapsedTime);
    DBMS_OUTPUT.PUT_LINE('==========================================================');
    DBMS_OUTPUT.PUT_LINE('MANUAL CLEANUP COMPLETED');
    DBMS_OUTPUT.PUT_LINE('==========================================================');
END;
/
