-- ===================================================================
-- MARS-1046 Test: Direct CREATE_EXTERNAL_TABLE Test
-- ===================================================================
-- Purpose: Test FILE_MANAGER.CREATE_EXTERNAL_TABLE procedure
--          and verify NORMALIZE_DATE_FORMAT functionality
-- Author: Grzegorz Michalski
-- Date: 2026-01-13
-- ===================================================================

SET SERVEROUTPUT ON;
SET LINESIZE 200;
SET PAGESIZE 100;

PROMPT ===================================================================
PROMPT MARS-1046: Testing CREATE_EXTERNAL_TABLE with ISO 8601 Format
PROMPT ===================================================================

-- Step 1: Upload test CSV file to INBOX (if not already there)
PROMPT
PROMPT Step 1: Verify CSV file in INBOX bucket
PROMPT File: INBOX/TEST/ISO8601/ISO8601_TEST_DATA/iso8601_test_data.csv
PROMPT (Upload manually using OCI CLI if needed)
PROMPT

-- Step 2: Create INBOX external table using FILE_MANAGER
PROMPT Step 2: Creating INBOX external table using ODS.FILE_MANAGER_ODS
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'ISO8601_TEST_DATA_INBOX',
        pTemplateTableName => 'CT_ET_TEMPLATES.ISO8601_TEST_DATA',
        pPrefix => 'INBOX/TEST/ISO8601/ISO8601_TEST_DATA',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvInboxBucketUri
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: External table ISO8601_TEST_DATA_INBOX created');
EXCEPTION
    WHEN OTHERS THEN
        IF SQLCODE = -955 THEN  -- ORA-00955: name is already used by an existing object
            DBMS_OUTPUT.PUT_LINE('INFO: External table ISO8601_TEST_DATA_INBOX already exists');
        ELSE
            DBMS_OUTPUT.PUT_LINE('ERROR: ' || SQLERRM);
            RAISE;
        END IF;
END;
/

-- Step 3: Verify external table exists
PROMPT
PROMPT Step 3: Verifying external table creation
SELECT 
    table_name,
    type_name,
    default_directory_name
FROM all_external_tables 
WHERE owner = 'ODS' 
  AND table_name = 'ISO8601_TEST_DATA_INBOX';

-- Step 4: Check ACCESS PARAMETERS - verify date format has quoted "T"
PROMPT
PROMPT Step 4: Checking ACCESS PARAMETERS (DATE FORMAT verification)
PROMPT Expected: MASK should contain YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM
PROMPT -------------------------------------------------------------------
SET LONG 50000
SET PAGESIZE 0
SELECT access_parameters 
FROM all_external_tables 
WHERE owner = 'ODS' 
  AND table_name = 'ISO8601_TEST_DATA_INBOX';
SET PAGESIZE 100

-- Step 5: Read data from external table
PROMPT
PROMPT ===================================================================
PROMPT Step 5: Reading data from INBOX external table
PROMPT ===================================================================
COLUMN EVENT_NAME FORMAT A30
COLUMN PARSED_TIMESTAMP FORMAT A40
COLUMN DESCRIPTION FORMAT A50

SELECT
    ID,
    EVENT_NAME,
    TO_CHAR(EVENT_TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS.FF3 TZH:TZM') AS PARSED_TIMESTAMP,
    DESCRIPTION
FROM ODS.ISO8601_TEST_DATA_INBOX
ORDER BY ID;

-- Step 6: Verify all records parsed correctly
PROMPT
PROMPT Step 6: Verification Summary
SELECT 
    COUNT(*) AS TOTAL_RECORDS,
    COUNT(EVENT_TIMESTAMP) AS PARSED_TIMESTAMPS,
    CASE 
        WHEN COUNT(*) = COUNT(EVENT_TIMESTAMP) THEN 'PASS'
        ELSE 'FAIL'
    END AS TEST_RESULT
FROM ODS.ISO8601_TEST_DATA_INBOX;

PROMPT
PROMPT ===================================================================
PROMPT Test Complete!
PROMPT ===================================================================
PROMPT
PROMPT Expected Results:
PROMPT 1. External table ISO8601_TEST_DATA_INBOX created in ODS schema
PROMPT 2. ACCESS PARAMETERS contain: YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM
PROMPT 3. All 5 records successfully parsed with timestamps
PROMPT 4. Milliseconds (.FF3) and timezone (TZH:TZM) preserved
PROMPT
PROMPT CRITICAL VERIFICATION:
PROMPT - Check ACCESS PARAMETERS output for quoted "T" separator
PROMPT - Verify all EVENT_TIMESTAMP values are not NULL
PROMPT - Confirm fractional seconds and timezone are present
PROMPT ===================================================================
