-- =====================================================================================
-- Script: 97_MARS_826_rollback_QR_ADJ_tables.sql
-- Purpose: Rollback QR_ADJ tables export - delete files from HIST bucket
-- Author: Grzegorz Michalski
-- Created: 2025-12-02
-- MARS Issue: MARS-826
-- Target Tables: 
--   - OU_LM.QR_ADJ_HEADER (~3,121 records)
--   - OU_LM.QR_ADJ_ITEM (~29,576 records)
--   - OU_LM.QR_ADJ_ITEM_HEADER (~29,876 records)
-- Target Location: mrds_hist_dev/ARCHIVE/LM/LM_QRE_ADJUSTMENTS_*
-- =====================================================================================

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

PROMPT =====================================================================================
PROMPT MARS-826 Rollback: Deleting QR_ADJ table exports from HIST bucket
PROMPT =====================================================================================

DECLARE
    vBucketUri VARCHAR2(500);
    vCredentialName VARCHAR2(100);
    vFileCount NUMBER := 0;
    vDeletedCount NUMBER := 0;
    vTotalDeleted NUMBER := 0;
    
    TYPE t_folder_list IS TABLE OF VARCHAR2(200);
    vFolders t_folder_list;
BEGIN
    -- Get bucket URI and credential from FILE_MANAGER configuration
    vBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('ARCHIVE');
    vCredentialName := CT_MRDS.ENV_MANAGER.gvCredentialName;
    
    DBMS_OUTPUT.PUT_LINE('START TIME: ' || TO_CHAR(SYSTIMESTAMP, 'YYYY-MM-DD HH24:MI:SS.FF3'));
    DBMS_OUTPUT.PUT_LINE('');
    
    -- Initialize folder list for QR_ADJ tables
    vFolders := t_folder_list(
        'ARCHIVE/LM/LM_QRE_ADJUSTMENTS_HEADER/',
        'ARCHIVE/LM/LM_QRE_ADJUSTMENTS_ITEM/',
        'ARCHIVE/LM/LM_QRE_ADJUSTMENTS_ITEM_HEADER/'
    );
    
    -- Process each folder
    FOR i IN 1..vFolders.COUNT LOOP
        DBMS_OUTPUT.PUT_LINE('Processing folder: ' || vFolders(i));
        vFileCount := 0;
        vDeletedCount := 0;
        
        -- List and delete all Parquet files in the folder
        FOR rec IN (
            SELECT object_name
            FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
                credential_name => vCredentialName,
                location_uri => vBucketUri || vFolders(i)
            ))
            WHERE object_name LIKE '%.parquet'
            ORDER BY object_name
        ) LOOP
            vFileCount := vFileCount + 1;
            
            BEGIN
                -- Delete the Parquet file
                DBMS_CLOUD.DELETE_OBJECT(
                    credential_name => vCredentialName,
                    object_uri => vBucketUri || vFolders(i) || rec.object_name
                );
                
                vDeletedCount := vDeletedCount + 1;
                DBMS_OUTPUT.PUT_LINE('  [' || vDeletedCount || '] Deleted: ' || rec.object_name);
                
            EXCEPTION
                WHEN OTHERS THEN
                    DBMS_OUTPUT.PUT_LINE('  ERROR deleting ' || rec.object_name || ': ' || SQLERRM);
            END;
        END LOOP;
        
        vTotalDeleted := vTotalDeleted + vDeletedCount;
        DBMS_OUTPUT.PUT_LINE('Folder summary: Found ' || vFileCount || ' files, deleted ' || vDeletedCount || ' files');
        DBMS_OUTPUT.PUT_LINE('');
        
    END LOOP;
    
    DBMS_OUTPUT.PUT_LINE('=====================================================================================');
    DBMS_OUTPUT.PUT_LINE('ROLLBACK COMPLETE: Total files deleted: ' || vTotalDeleted);
    DBMS_OUTPUT.PUT_LINE('END TIME: ' || TO_CHAR(SYSTIMESTAMP, 'YYYY-MM-DD HH24:MI:SS.FF3'));
    DBMS_OUTPUT.PUT_LINE('=====================================================================================');
    
EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('');
        DBMS_OUTPUT.PUT_LINE('FATAL ERROR: ' || SQLERRM);
        DBMS_OUTPUT.PUT_LINE('Error occurred at: ' || TO_CHAR(SYSTIMESTAMP, 'YYYY-MM-DD HH24:MI:SS.FF3'));
        RAISE;
END;
/
