-- ===================================================================
-- MARS-1005 Verify Exports: Check Export Results and File Creation
-- ===================================================================
-- Purpose: Verify that OU_TOP historical data export completed successfully
-- Author: Grzegorz Michalski
-- Date: 2026-03-06
-- MARS Issue: MARS-1005
-- Tables: 6 OU_TOP.LEGACY_* tables exported to ODS/TOP/ bucket paths

SET SERVEROUTPUT ON SIZE UNLIMITED
SET TIMING ON

PROMPT =========================================================================
PROMPT MARS-1005 Export Verification
PROMPT =========================================================================

-- Check 1: Verify files were registered in A_SOURCE_FILE_RECEIVED
PROMPT Checking export file registration (PROCESS_NAME = MARS-1005)...
DECLARE
    vFileCount   NUMBER := 0;
    vTotalBytes  NUMBER := 0;
BEGIN
    SELECT COUNT(*), NVL(SUM(BYTES), 0)
    INTO vFileCount, vTotalBytes
    FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
    WHERE PROCESS_NAME = 'MARS-1005'
      AND RECEPTION_DATE >= SYSDATE - 1/24;  -- Last hour

    DBMS_OUTPUT.PUT_LINE('Registered export files (last hour): ' || vFileCount);
    DBMS_OUTPUT.PUT_LINE('Total file size: ' || ROUND(vTotalBytes / 1024, 2) || ' KB');

    IF vFileCount = 0 THEN
        DBMS_OUTPUT.PUT_LINE('WARNING: No export files found in registration');
    ELSIF vFileCount < 6 THEN
        DBMS_OUTPUT.PUT_LINE('WARNING: Expected at least 6 files (1 per table), found: ' || vFileCount);
    ELSE
        DBMS_OUTPUT.PUT_LINE('SUCCESS: All expected export files registered (>= 6)');
    END IF;
END;
/

-- Check 2: Show recent export registrations by table
PROMPT Recent export file registrations per table:
SELECT
    A_SOURCE_FILE_CONFIG_KEY                              AS CONFIG_KEY,
    SUBSTR(SOURCE_FILE_NAME, 1, 55)                       AS FILE_NAME,
    PROCESSING_STATUS,
    ROUND(NVL(BYTES, 0) / 1024, 2)                       AS SIZE_KB,
    TO_CHAR(RECEPTION_DATE, 'HH24:MI:SS')                AS TIME_EXPORTED
FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
WHERE PROCESS_NAME = 'MARS-1005'
  AND RECEPTION_DATE >= SYSDATE - 1/24
ORDER BY A_SOURCE_FILE_CONFIG_KEY, RECEPTION_DATE DESC;

-- Check 2b: File count per A_SOURCE_FILE_CONFIG_KEY
PROMPT Export file count per source config key:
SELECT
    r.A_SOURCE_FILE_CONFIG_KEY,
    c.TABLE_ID,
    COUNT(*)                                              AS FILE_COUNT,
    ROUND(NVL(SUM(r.BYTES), 0) / 1024, 2)               AS TOTAL_KB
FROM CT_MRDS.A_SOURCE_FILE_RECEIVED r
JOIN CT_MRDS.A_SOURCE_FILE_CONFIG  c
    ON r.A_SOURCE_FILE_CONFIG_KEY = c.A_SOURCE_FILE_CONFIG_KEY
WHERE r.PROCESS_NAME = 'MARS-1005'
  AND r.RECEPTION_DATE >= SYSDATE - 1/24
GROUP BY r.A_SOURCE_FILE_CONFIG_KEY, c.TABLE_ID
ORDER BY r.A_SOURCE_FILE_CONFIG_KEY;

-- Check 3: Verify export process logs
PROMPT Checking export process logs...
DECLARE
    vLogCount NUMBER := 0;
    vErrorCount NUMBER := 0;
BEGIN
    SELECT COUNT(*), SUM(CASE WHEN LOG_LEVEL = 'ERROR' THEN 1 ELSE 0 END)
    INTO vLogCount, vErrorCount
    FROM CT_MRDS.A_PROCESS_LOG
    WHERE PROCESS_NAME = 'MARS-1005'
      AND LOG_TIMESTAMP >= SYSTIMESTAMP - INTERVAL '1' HOUR;
    
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Process log entries: ' || vLogCount);
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Error entries: ' || vErrorCount);
    
    IF vErrorCount > 0 THEN
        DBMS_OUTPUT.PUT_LINE('WARNING: ' || vErrorCount || ' errors found in process log');
    ELSE
        DBMS_OUTPUT.PUT_LINE('SUCCESS: No errors found in process log');
    END IF;
END;
/

-- Check 4: Display recent process logs
PROMPT Recent MARS-1005 process logs:
SELECT 
    TO_CHAR(LOG_TIMESTAMP, 'HH24:MI:SS') AS TIME,
    PROCEDURE_NAME,
    LOG_LEVEL,
    SUBSTR(LOG_MESSAGE, 1, 60) AS MESSAGE
FROM CT_MRDS.A_PROCESS_LOG
WHERE PROCESS_NAME = 'MARS-1005'
  AND LOG_TIMESTAMP >= SYSTIMESTAMP - INTERVAL '1' HOUR
ORDER BY LOG_TIMESTAMP DESC
FETCH FIRST 10 ROWS ONLY;

-- Check 5: Cloud bucket file verification across all 6 TOP folders
PROMPT Checking cloud bucket files in ODS/TOP/ paths...
DECLARE
    vCredentialName VARCHAR2(100) := 'OCI$RESOURCE_PRINCIPAL';
    vDataBucketUri  VARCHAR2(500);
    vTotalFiles     NUMBER := 0;

    TYPE t_folder IS TABLE OF VARCHAR2(200);
    vFolders t_folder := t_folder(
        'ODS/TOP/TOP_ALLOTMENT/',
        'ODS/TOP/TOP_ALLOTMENT_MODIFICATION_HEADER/',
        'ODS/TOP/TOP_ALLOTMENT_MODIFICATION_ITEM/',
        'ODS/TOP/TOP_ANNOUNCEMENT/',
        'ODS/TOP/TOP_FULLBIDLIST_ITEM/',
        'ODS/TOP/TOP_FULLBID_ARRAY_COMPILED/'
    );

    vFolderFiles NUMBER;
    vFolderSize  NUMBER;
BEGIN
    vDataBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('DATA');
    DBMS_OUTPUT.PUT_LINE('Bucket URI: ' || vDataBucketUri);
    DBMS_OUTPUT.PUT_LINE('');
    DBMS_OUTPUT.PUT_LINE(RPAD('Folder', 55) || RPAD('Files', 8) || 'Total KB');
    DBMS_OUTPUT.PUT_LINE(RPAD('-', 75, '-'));

    FOR i IN 1..vFolders.COUNT LOOP
        vFolderFiles := 0;
        vFolderSize  := 0;
        BEGIN
            SELECT COUNT(*), NVL(SUM(bytes), 0)
            INTO vFolderFiles, vFolderSize
            FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
                credential_name => vCredentialName,
                location_uri    => vDataBucketUri || vFolders(i)
            ))
            WHERE object_name NOT LIKE '%/';
        EXCEPTION WHEN OTHERS THEN NULL;
        END;

        DBMS_OUTPUT.PUT_LINE(
            RPAD(vFolders(i), 55) ||
            RPAD(TO_CHAR(vFolderFiles), 8) ||
            ROUND(vFolderSize / 1024, 2) || ' KB'
        );
        vTotalFiles := vTotalFiles + vFolderFiles;
    END LOOP;

    DBMS_OUTPUT.PUT_LINE(RPAD('-', 75, '-'));
    DBMS_OUTPUT.PUT_LINE('Total files across all TOP folders: ' || vTotalFiles);

    IF vTotalFiles = 0 THEN
        DBMS_OUTPUT.PUT_LINE('WARNING: No files found in any TOP folder');
    ELSE
        DBMS_OUTPUT.PUT_LINE('SUCCESS: Files present in ODS/TOP/ bucket paths');
    END IF;
EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('WARNING: Cannot access cloud bucket: ' || SQLERRM);
END;
/

PROMPT
PROMPT =========================================================================
PROMPT MARS-1005 Export Verification Summary
PROMPT =========================================================================

DECLARE
    vFileRegCount  NUMBER := 0;
    vLogErrorCount NUMBER := 0;
    vOverallStatus VARCHAR2(20);
BEGIN
    SELECT COUNT(*)
    INTO vFileRegCount
    FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
    WHERE PROCESS_NAME = 'MARS-1005'
      AND RECEPTION_DATE >= SYSDATE - 1/24;

    SELECT COUNT(*)
    INTO vLogErrorCount
    FROM CT_MRDS.A_PROCESS_LOG
    WHERE PROCESS_NAME = 'MARS-1005'
      AND LOG_LEVEL = 'ERROR'
      AND LOG_TIMESTAMP >= SYSTIMESTAMP - INTERVAL '1' HOUR;

    IF vFileRegCount >= 6 AND vLogErrorCount = 0 THEN
        vOverallStatus := 'SUCCESS';
    ELSIF vFileRegCount > 0 AND vLogErrorCount = 0 THEN
        vOverallStatus := 'PARTIAL SUCCESS';
    ELSE
        vOverallStatus := 'ISSUES DETECTED';
    END IF;

    DBMS_OUTPUT.PUT_LINE('MARS-1005 Export Verification: ' || vOverallStatus);
    DBMS_OUTPUT.PUT_LINE('- Registered files (last hour): ' || vFileRegCount || ' (expected: >= 6, one per table)');
    DBMS_OUTPUT.PUT_LINE('- Process errors: ' || vLogErrorCount);

    IF vOverallStatus = 'SUCCESS' THEN
        DBMS_OUTPUT.PUT_LINE('SUCCESS: All validations passed - export successful');
    ELSIF vOverallStatus = 'PARTIAL SUCCESS' THEN
        DBMS_OUTPUT.PUT_LINE('WARNING: Some tables may have incomplete exports - review registrations above');
    ELSE
        DBMS_OUTPUT.PUT_LINE('ISSUES DETECTED: Review process logs and bucket contents above');
    END IF;
END;
/

PROMPT =========================================================================
PROMPT Export Verification Completed
PROMPT =========================================================================