-- ===================================================================
-- MARS-956 Verify Exports: Check Export Results and File Creation
-- ===================================================================
-- Purpose: Verify that C2D MPEC export completed successfully
-- Author: Grzegorz Michalski
-- Date: 2026-02-12

SET SERVEROUTPUT ON SIZE UNLIMITED
SET TIMING ON

PROMPT =========================================================================
PROMPT MARS-956 Export Verification
PROMPT =========================================================================

-- Check 1: Verify files were registered in A_SOURCE_FILE_RECEIVED
PROMPT Checking export file registration...
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 RECEPTION_DATE >= SYSDATE - 1/24  -- Last hour
      AND (SOURCE_FILE_NAME LIKE '2001_%'   -- MPEC_ADMIN ETL keys
        OR SOURCE_FILE_NAME LIKE '2002_%'
        OR SOURCE_FILE_NAME LIKE '2003_%'
        OR SOURCE_FILE_NAME LIKE '2004_%'
        OR SOURCE_FILE_NAME LIKE '2005_%'
        OR SOURCE_FILE_NAME LIKE '2006_%'   -- MPEC_CONTENT ETL keys
        OR SOURCE_FILE_NAME LIKE '2007_%'
        OR SOURCE_FILE_NAME LIKE '2008_%'
        OR SOURCE_FILE_NAME LIKE '2009_%'   -- MPEC_CONTENT_CRITERION ETL keys
        OR SOURCE_FILE_NAME LIKE '2010_%');
    
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Registered export files: ' || vFileCount);
    DBMS_OUTPUT.PUT_LINE('SUCCESS: 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 < 9 THEN
        DBMS_OUTPUT.PUT_LINE('WARNING: Expected 9 files (3 tables x 3 ETL keys), found: ' || vFileCount);
    ELSE
        DBMS_OUTPUT.PUT_LINE('SUCCESS: All expected export files found');
    END IF;
END;
/

-- Check 2: Show recent export registrations
PROMPT Recent export file registrations:
SELECT 
    SUBSTR(SOURCE_FILE_NAME, 1, 40) AS FILE_NAME,
    A_SOURCE_FILE_CONFIG_KEY AS CONFIG_KEY,
    PROCESSING_STATUS,
    ROUND(BYTES/1024, 2) AS SIZE_KB,
    TO_CHAR(RECEPTION_DATE, 'HH24:MI:SS') AS TIME_EXPORTED
FROM CT_MRDS.A_SOURCE_FILE_RECEIVED 
WHERE RECEPTION_DATE >= SYSDATE - 1/24  -- Last hour
  AND (SOURCE_FILE_NAME LIKE '200%')  -- ETL keys starting with 200
ORDER BY RECEPTION_DATE DESC;

-- 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-956'
      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-956 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-956'
  AND LOG_TIMESTAMP >= SYSTIMESTAMP - INTERVAL '1' HOUR
ORDER BY LOG_TIMESTAMP DESC
FETCH FIRST 10 ROWS ONLY;

-- Check 5: Cloud bucket file verification (if cloud_wrapper available)
PROMPT Checking cloud bucket files...
DECLARE
    vCloudFileCount NUMBER := 0;
    vCredentialName VARCHAR2(100);
    vDataBucketUri VARCHAR2(500);
BEGIN
    -- Get bucket URI and credential
    vDataBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('ODS');
    vCredentialName := CT_MRDS.ENV_MANAGER.gvCredentialName;
    
    DBMS_OUTPUT.PUT_LINE('Checking ODS bucket: ' || vDataBucketUri);
    
    -- Count files in cloud bucket
    BEGIN
        FOR rec IN (
            SELECT object_name
            FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
                credential_name => vCredentialName,
                location_uri => vDataBucketUri
            ))
            WHERE object_name LIKE 'ODS/C2D/C2D_MPEC_%'
        ) LOOP
            vCloudFileCount := vCloudFileCount + 1;
            IF vCloudFileCount <= 5 THEN  -- Show first 5 files
                DBMS_OUTPUT.PUT_LINE('- ' || rec.object_name);
            END IF;
        END LOOP;
        
        DBMS_OUTPUT.PUT_LINE('SUCCESS: Cloud bucket files found: ' || vCloudFileCount);
        
        IF vCloudFileCount = 0 THEN
            DBMS_OUTPUT.PUT_LINE('WARNING: No files found in cloud bucket');
        END IF;
        
    EXCEPTION
        WHEN OTHERS THEN
            DBMS_OUTPUT.PUT_LINE('WARNING: Cannot access cloud bucket: ' || SQLERRM);
    END;
END;
/

PROMPT 
PROMPT =========================================================================
PROMPT MARS-956 Export Verification Summary
PROMPT =========================================================================

-- Final verification summary
DECLARE
    vFileRegCount NUMBER := 0;
    vCloudFileCount NUMBER := 0;
    vLogErrorCount NUMBER := 0;
    vOverallStatus VARCHAR2(20);
BEGIN
    -- Count registered files
    SELECT COUNT(*)
    INTO vFileRegCount
    FROM CT_MRDS.A_SOURCE_FILE_RECEIVED 
    WHERE RECEPTION_DATE >= SYSDATE - 1/24
      AND SOURCE_FILE_NAME LIKE '200%';
    
    -- Count process errors
    SELECT COUNT(*)
    INTO vLogErrorCount
    FROM CT_MRDS.A_PROCESS_LOG
    WHERE PROCESS_NAME = 'MARS-956'
      AND LOG_LEVEL = 'ERROR'
      AND LOG_TIMESTAMP >= SYSTIMESTAMP - INTERVAL '1' HOUR;
    
    -- Determine overall status
    IF vFileRegCount >= 9 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-956 Export Verification: ' || vOverallStatus);
    DBMS_OUTPUT.PUT_LINE('- Registered files: ' || vFileRegCount || ' (expected: 9)');
    DBMS_OUTPUT.PUT_LINE('- Process errors: ' || vLogErrorCount);
    
    IF vOverallStatus = 'SUCCESS' THEN
        DBMS_OUTPUT.PUT_LINE('SUCCESS: All validations passed - export successful');
    ELSE
        DBMS_OUTPUT.PUT_LINE('WARNING: Some issues detected - review logs');
    END IF;
END;
/

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