SET SERVEROUTPUT ON SIZE UNLIMITED
SET DEFINE OFF

DECLARE
    vCredential VARCHAR2(100) := 'OCI$RESOURCE_PRINCIPAL';
    vDataBucket VARCHAR2(200) := 'https://objectstorage.eu-frankfurt-1.oraclecloud.com/n/frtgjxu7zl7c/b/data/o/';
    vArchiveBucket VARCHAR2(200) := 'https://objectstorage.eu-frankfurt-1.oraclecloud.com/n/frtgjxu7zl7c/b/history/o/';
    vCount NUMBER := 0;
BEGIN
    DBMS_OUTPUT.PUT_LINE('=== Checking CSV files in DATA bucket ===');
    FOR rec IN (
        SELECT object_name
        FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
            credential_name => vCredential,
            location_uri => vDataBucket || 'ODS/CSDB/CSDB_DEBT/'
        ))
        WHERE object_name LIKE 'LEGACY_DEBT%'
    ) LOOP
        vCount := vCount + 1;
        DBMS_OUTPUT.PUT_LINE('  [' || vCount || '] ' || rec.object_name);
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('Total CSV files DEBT: ' || vCount);
    
    vCount := 0;
    FOR rec IN (
        SELECT object_name
        FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
            credential_name => vCredential,
            location_uri => vDataBucket || 'ODS/CSDB/CSDB_DEBT_DAILY/'
        ))
        WHERE object_name LIKE 'LEGACY_DEBT_DAILY%'
    ) LOOP
        vCount := vCount + 1;
        DBMS_OUTPUT.PUT_LINE('  [' || vCount || '] ' || rec.object_name);
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('Total CSV files DEBT_DAILY: ' || vCount);
    
    DBMS_OUTPUT.PUT_LINE(CHR(10) || '=== Checking Parquet files in ARCHIVE bucket ===');
    vCount := 0;
    FOR rec IN (
        SELECT object_name
        FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
            credential_name => vCredential,
            location_uri => vArchiveBucket || 'ARCHIVE/CSDB/CSDB_DEBT/'
        ))
        WHERE ROWNUM <= 5
    ) LOOP
        vCount := vCount + 1;
        DBMS_OUTPUT.PUT_LINE('  [' || vCount || '] ' || rec.object_name);
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('Total Parquet files DEBT (first 5): ' || vCount);
    
    vCount := 0;
    FOR rec IN (
        SELECT object_name
        FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
            credential_name => vCredential,
            location_uri => vArchiveBucket || 'ARCHIVE/CSDB/CSDB_DEBT_DAILY/'
        ))
    ) LOOP
        vCount := vCount + 1;
        DBMS_OUTPUT.PUT_LINE('  [' || vCount || '] ' || rec.object_name);
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('Total Parquet files DEBT_DAILY: ' || vCount);
    
    DBMS_OUTPUT.PUT_LINE(CHR(10) || '=== Now testing DELETE_OBJECT ===');
    DBMS_OUTPUT.PUT_LINE('Testing delete for: ODS/CSDB/CSDB_DEBT/LEGACY_DEBT_202510_1_20260213T092239041072Z.csv');
    
    BEGIN
        DBMS_CLOUD.DELETE_OBJECT(
            credential_name => vCredential,
            object_uri => vDataBucket || 'ODS/CSDB/CSDB_DEBT/LEGACY_DEBT_202510_1_20260213T092239041072Z.csv'
        );
        DBMS_OUTPUT.PUT_LINE('SUCCESS: File deleted');
    EXCEPTION
        WHEN OTHERS THEN
            DBMS_OUTPUT.PUT_LINE('ERROR: ' || SQLERRM);
    END;
END;
/
