--=============================================================================================================================
-- MARS-835 ROLLBACK: Delete Group 1 Exported Files (DEBT, DEBT_DAILY)
--=============================================================================================================================
-- Purpose: Delete exported Parquet files from HIST bucket (ARCHIVE only)
-- WARNING: This will permanently delete exported data files!
-- Author: Grzegorz Michalski
-- Date: 2025-12-17
-- Updated: 2026-02-24 (Changed to HIST-only rollback, no DATA bucket)
-- Related: MARS-835 - CSDB Data Export Rollback
--=============================================================================================================================

SET SERVEROUTPUT ON SIZE UNLIMITED

PROMPT ========================================================================
PROMPT ROLLBACK: Deleting DEBT exported files from HIST
PROMPT ========================================================================
PROMPT WARNING: This will delete files from:
PROMPT   - HIST bucket: mrds_hist_dev/ARCHIVE/CSDB/CSDB_DEBT/
PROMPT ========================================================================

DECLARE
    vHistBucketUri VARCHAR2(500);
    vCredentialName VARCHAR2(100);
    vFileCount NUMBER := 0;
BEGIN
    -- Get bucket URI and credential
    vHistBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('ARCHIVE');
    vCredentialName := CT_MRDS.ENV_MANAGER.gvCredentialName;
    
    DBMS_OUTPUT.PUT_LINE('Deleting DEBT Parquet files from ARCHIVE bucket...');
    DBMS_OUTPUT.PUT_LINE('  Using DBMS_CLOUD.LIST_OBJECTS');
    
    -- Delete Parquet files from ARCHIVE bucket using DBMS_CLOUD.LIST_OBJECTS
    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
                    RAISE;
                END IF;
        END;
    END LOOP;
    
    IF vFileCount = 0 THEN
        DBMS_OUTPUT.PUT_LINE('  INFO: No DEBT Parquet files found to delete');
    END IF;
    
    DBMS_OUTPUT.PUT_LINE('SUCCESS: DEBT Parquet files deleted from ARCHIVE bucket (' || vFileCount || ' file(s))');
END;
/

PROMPT ========================================================================
PROMPT ROLLBACK: Deleting DEBT_DAILY exported files from HIST
PROMPT ========================================================================
PROMPT WARNING: This will delete files from:
PROMPT   - HIST bucket: mrds_hist_dev/ARCHIVE/CSDB/CSDB_DEBT_DAILY/
PROMPT ========================================================================

DECLARE
    vHistBucketUri VARCHAR2(500);
    vCredentialName VARCHAR2(100);
    vFileCount NUMBER := 0;
BEGIN
    -- Get bucket URI and credential
    vHistBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('ARCHIVE');
    vCredentialName := CT_MRDS.ENV_MANAGER.gvCredentialName;
    
    DBMS_OUTPUT.PUT_LINE('Deleting DEBT_DAILY Parquet files from ARCHIVE bucket...');
    DBMS_OUTPUT.PUT_LINE('  Using DBMS_CLOUD.LIST_OBJECTS');
    
    -- Delete Parquet files from ARCHIVE bucket using DBMS_CLOUD.LIST_OBJECTS
    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
                    RAISE;
                END IF;
        END;
    END LOOP;
    
    IF vFileCount = 0 THEN
        DBMS_OUTPUT.PUT_LINE('  INFO: No DEBT_DAILY Parquet files found to delete');
    END IF;
    
    DBMS_OUTPUT.PUT_LINE('SUCCESS: DEBT_DAILY Parquet files deleted from ARCHIVE bucket (' || vFileCount || ' file(s))');
END;
/

PROMPT SUCCESS: Group 1 rollback completed

--=============================================================================================================================
-- End of Script
--=============================================================================================================================
