-- ===================================================================
-- MARS-1046 Test: Configure FILE_MANAGER for ISO 8601 Testing
-- ===================================================================
-- Purpose: Configure source system and date format for ISO 8601 test
-- Author: Grzegorz Michalski
-- Date: 2025-11-27
-- ===================================================================

SET SERVEROUTPUT ON;

PROMPT ===================================================================
PROMPT Configuring ISO 8601 Test Source System
PROMPT ===================================================================

-- Step 1: Add test source (if not exists)
BEGIN
    CT_MRDS.FILE_MANAGER.ADD_SOURCE(
        pSourceKey => 'TEST',
        pSourceName => 'Test Data Source for MARS-1046'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Source TEST created');
EXCEPTION
    WHEN OTHERS THEN
        IF SQLCODE = -1 THEN  -- ORA-00001: unique constraint violated
            DBMS_OUTPUT.PUT_LINE('INFO: Source TEST already exists');
        ELSE
            RAISE;
        END IF;
END;
/

-- Step 2: Configure source file for ISO8601_TEST_DATA
BEGIN
    CT_MRDS.FILE_MANAGER.ADD_SOURCE_FILE_CONFIG(
        pSourceKey => 'TEST',
        pSourceFileType => 'INPUT',
        pSourceFileId => 'ISO8601',
        pSourceFileDesc => 'ISO 8601 Date Format Test Data',
        pSourceFileNamePattern => 'iso8601_test_*.csv',
        pTableId => 'ISO8601_TEST_DATA',
        pTemplateTableName => 'CT_ET_TEMPLATES.ISO8601_TEST_DATA',
        pEncoding => 'UTF8'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Source file config created for ISO8601_TEST_DATA');
EXCEPTION
    WHEN OTHERS THEN
        IF SQLCODE = -1 THEN
            DBMS_OUTPUT.PUT_LINE('INFO: Source file config already exists');
        ELSE
            RAISE;
        END IF;
END;
/

-- Step 3: Configure ISO 8601 date format with T separator
BEGIN
    CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT(
        pTemplateTableName => 'CT_ET_TEMPLATES.ISO8601_TEST_DATA',
        pColumnName => 'EVENT_TIMESTAMP',
        pDateFormat => 'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM'
    );
    DBMS_OUTPUT.PUT_LINE('SUCCESS: Date format configured: YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM (full ISO 8601)');
EXCEPTION
    WHEN OTHERS THEN
        IF SQLCODE = -1 THEN
            DBMS_OUTPUT.PUT_LINE('INFO: Date format already configured');
        ELSE
            RAISE;
        END IF;
END;
/

-- Step 4: Verify configuration
PROMPT
PROMPT ===================================================================
PROMPT Configuration Summary:
PROMPT ===================================================================

SELECT 
    A_SOURCE_KEY,
    SOURCE_FILE_ID,
    TABLE_ID,
    TEMPLATE_TABLE_NAME,
    SOURCE_FILE_NAME_PATTERN
FROM CT_MRDS.A_SOURCE_FILE_CONFIG
WHERE A_SOURCE_KEY = 'TEST'
  AND SOURCE_FILE_ID = 'ISO8601';

PROMPT
PROMPT Date Format Configuration:
SELECT 
    TEMPLATE_TABLE_NAME,
    COLUMN_NAME,
    DATE_FORMAT
FROM CT_MRDS.A_COLUMN_DATE_FORMAT
WHERE TEMPLATE_TABLE_NAME = 'CT_ET_TEMPLATES.ISO8601_TEST_DATA';

PROMPT
PROMPT ===================================================================
PROMPT Configuration Complete!
PROMPT ===================================================================
PROMPT
PROMPT Next: Create CSV file and upload to OCI bucket
PROMPT Path pattern: INBOX/TEST/ISO8601/ISO8601_TEST_DATA/iso8601_test_*.csv
PROMPT ===================================================================
