-- ===================================================================
-- MARS-1046 Test: Process ISO 8601 Test File
-- ===================================================================
-- Purpose: Process test CSV file and verify ISO 8601 date parsing
-- Author: Grzegorz Michalski
-- Date: 2025-11-27
-- ===================================================================

SET SERVEROUTPUT ON SIZE UNLIMITED;

PROMPT ===================================================================
PROMPT Processing ISO 8601 Test File
PROMPT ===================================================================

-- Process the uploaded file
BEGIN
    CT_MRDS.FILE_MANAGER.PROCESS_SOURCE_FILE(
        pSourceFileReceivedName => 'INBOX/TEST/ISO8601/ISO8601_TEST_DATA/iso8601_test_data.csv'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: File processed successfully');
EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('ERROR: ' || SQLERRM);
        RAISE;
END;
/

PROMPT
PROMPT ===================================================================
PROMPT Checking File Status
PROMPT ===================================================================

SELECT 
    SOURCE_FILE_NAME,
    PROCESSING_STATUS,
    RECEPTION_DATE
FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
WHERE SOURCE_FILE_NAME LIKE '%iso8601_test_data.csv%'
ORDER BY RECEPTION_DATE DESC
FETCH FIRST 1 ROW ONLY;

PROMPT
PROMPT ===================================================================
PROMPT Reading Data from External Table (ODS bucket)
PROMPT ===================================================================

-- Query the external table to verify date parsing
SELECT 
    ID,
    EVENT_NAME,
    TO_CHAR(EVENT_TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS') AS PARSED_TIMESTAMP,
    EVENT_TIMESTAMP,
    DESCRIPTION
FROM ODS.ISO8601_TEST_DATA_ODS
ORDER BY ID;

PROMPT
PROMPT ===================================================================
PROMPT Test Complete!
PROMPT ===================================================================
PROMPT
PROMPT Expected Results:
PROMPT - All 5 records should be visible
PROMPT - EVENT_TIMESTAMP should be properly parsed from ISO 8601 format
PROMPT - Original format: 2012-03-02T14:16:23.798+01:00
PROMPT - Parsed format: 2012-03-02 14:16:23 (timezone converted)
PROMPT ===================================================================
