-- =====================================================================================
-- Script: 00_MARS_826_pre_check_existing_files.sql
-- Purpose: Display existing archive files in HIST bucket before export
-- Author: Grzegorz Michalski
-- Created: 2025-12-02
-- MARS Issue: MARS-826
-- Target Location: mrds_hist_dev/ARCHIVE/LM/
-- =====================================================================================

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

PROMPT =====================================================================================
PROMPT MARS-826 Pre-Check: Listing existing archive files in HIST bucket
PROMPT =====================================================================================

DECLARE
    vBucketUri VARCHAR2(500);
    vCredentialName VARCHAR2(100);
    vFileCount NUMBER := 0;
    vTotalFiles 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('CHECK TIME: ' || TO_CHAR(SYSTIMESTAMP, 'YYYY-MM-DD HH24:MI:SS.FF3'));
    DBMS_OUTPUT.PUT_LINE('Bucket URI: ' || vBucketUri);
    DBMS_OUTPUT.PUT_LINE('Credential: ' || vCredentialName);
    DBMS_OUTPUT.PUT_LINE('');
    
    -- Initialize folder list for all 19 tables
    vFolders := t_folder_list(
        'ARCHIVE/LM/LM_ADHOC_ADJUSTMENTS_HEADER/',
        'ARCHIVE/LM/LM_ADHOC_ADJUSTMENTS_ITEM/',
        'ARCHIVE/LM/LM_ADHOC_ADJUSTMENTS_ITEM_HEADER/',
        'ARCHIVE/LM/LM_BALANCESHEET_HEADER/',
        'ARCHIVE/LM/LM_BALANCESHEET_ITEM/',
        'ARCHIVE/LM/LM_CSM_ADJUSTMENTS_HEADER/',
        'ARCHIVE/LM/LM_CSM_ADJUSTMENTS_ITEM/',
        'ARCHIVE/LM/LM_CSM_ADJUSTMENTS_ITEM_HEADER/',
        'ARCHIVE/LM/LM_STANDING_FACILITIES/',
        'ARCHIVE/LM/LM_STANDING_FACILITIES_HEADER/',
        'ARCHIVE/LM/LM_CURRENT_ACCOUNTS_HEADER/',
        'ARCHIVE/LM/LM_CURRENT_ACCOUNTS_ITEM/',
        'ARCHIVE/LM/LM_FORECAST_HEADER/',
        'ARCHIVE/LM/LM_FORECAST_ITEM/',
        'ARCHIVE/LM/LM_QRE_ADJUSTMENTS_HEADER/',
        'ARCHIVE/LM/LM_QRE_ADJUSTMENTS_ITEM/',
        'ARCHIVE/LM/LM_QRE_ADJUSTMENTS_ITEM_HEADER/',
        'ARCHIVE/LM/LM_TTS_HEADER/',
        'ARCHIVE/LM/LM_TTS_ITEM/'
    );
    
    -- Check each folder
    FOR i IN 1..vFolders.COUNT LOOP
        vFileCount := 0;
        
        -- List all Parquet files in the folder
        FOR rec IN (
            SELECT object_name, bytes
            FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
                credential_name => vCredentialName,
                location_uri => vBucketUri || vFolders(i)
            ))
            WHERE object_name LIKE '%.parquet'
            ORDER BY object_name
        ) LOOP
            IF vFileCount = 0 THEN
                DBMS_OUTPUT.PUT_LINE('Folder: ' || vFolders(i));
            END IF;
            
            vFileCount := vFileCount + 1;
            DBMS_OUTPUT.PUT_LINE('  [' || vFileCount || '] ' || rec.object_name || ' (' || 
                                ROUND(rec.bytes/1024/1024, 2) || ' MB)');
        END LOOP;
        
        IF vFileCount > 0 THEN
            DBMS_OUTPUT.PUT_LINE('Folder summary: ' || vFileCount || ' files found');
            DBMS_OUTPUT.PUT_LINE('');
            vTotalFiles := vTotalFiles + vFileCount;
        END IF;
    END LOOP;
    
    DBMS_OUTPUT.PUT_LINE('=====================================================================================');
    DBMS_OUTPUT.PUT_LINE('PRE-CHECK COMPLETE: Total existing files: ' || vTotalFiles);
    IF vTotalFiles = 0 THEN
        DBMS_OUTPUT.PUT_LINE('STATUS: No existing archive files found - ready for initial export');
    ELSE
        DBMS_OUTPUT.PUT_LINE('WARNING: Existing archive files found - export will add new files or overwrite existing ones');
    END IF;
    DBMS_OUTPUT.PUT_LINE('=====================================================================================');
    
EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('');
        DBMS_OUTPUT.PUT_LINE('ERROR: ' || SQLERRM);
        RAISE;
END;
/
