-- ===================================================================
-- MARS-1046 Test Script: ISO 8601 Date Format Normalization
-- ===================================================================
-- Purpose: Test NORMALIZE_DATE_FORMAT function and external table integration
-- Author: Grzegorz Michalski
-- Date: 2025-11-27
-- ===================================================================

SET SERVEROUTPUT ON;

PROMPT ========================================
PROMPT MARS-1046: Test Suite
PROMPT ========================================

-- Test 1: Verify package version
PROMPT
PROMPT Test 1: Verify FILE_MANAGER version
SELECT FILE_MANAGER.GET_VERSION() AS VERSION FROM DUAL;
-- Expected: 3.3.1

-- Test 2: Check build info
PROMPT
PROMPT Test 2: Check build information
SELECT FILE_MANAGER.GET_BUILD_INFO() AS BUILD_INFO FROM DUAL;

-- Test 3: Verify package changes
PROMPT
PROMPT Test 3: Verify no untracked changes
SELECT CT_MRDS.ENV_MANAGER.CHECK_PACKAGE_CHANGES('CT_MRDS', 'FILE_MANAGER') AS STATUS FROM DUAL;
-- Expected: OK: Package CT_MRDS.FILE_MANAGER has not changed.

-- Test 4: Check compilation status
PROMPT
PROMPT Test 4: Check compilation status
SELECT object_name, object_type, status 
FROM ALL_OBJECTS 
WHERE OWNER = 'CT_MRDS'
  AND OBJECT_NAME = 'FILE_MANAGER';
-- Expected: Both PACKAGE and PACKAGE BODY with STATUS = VALID

-- Test 5: Manual test of NORMALIZE_DATE_FORMAT logic
PROMPT
PROMPT Test 5: Manual format normalization tests
PROMPT
PROMPT Test Case 1: ISO 8601 with T separator
PROMPT Input:  YYYY-MM-DDTHH24:MI:SS.FF3TZH:TZM
PROMPT Expected: YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM
PROMPT
DECLARE
   vInput VARCHAR2(200) := 'YYYY-MM-DDTHH24:MI:SS.FF3TZH:TZM';
   vExpected VARCHAR2(200) := 'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM';
   vActual VARCHAR2(200);
BEGIN
   -- Simulate NORMALIZE_DATE_FORMAT logic
   vActual := vInput;
   IF INSTR(vActual, '"T"') = 0 AND REGEXP_LIKE(vActual, '[YMD]T[HM]') THEN
      vActual := REGEXP_REPLACE(vActual, '([YMD])T([HM])', '\1"T"\2');
   END IF;
   
   DBMS_OUTPUT.PUT_LINE('Input:    ' || vInput);
   DBMS_OUTPUT.PUT_LINE('Expected: ' || vExpected);
   DBMS_OUTPUT.PUT_LINE('Actual:   ' || vActual);
   DBMS_OUTPUT.PUT_LINE('Result:   ' || CASE WHEN vActual = vExpected THEN 'PASS' ELSE 'FAIL' END);
END;
/

PROMPT
PROMPT Test Case 2: Standard format (no change expected)
PROMPT Input:  DD/MM/YYYY HH24:MI:SS
PROMPT Expected: DD/MM/YYYY HH24:MI:SS (unchanged)
PROMPT
DECLARE
   vInput VARCHAR2(200) := 'DD/MM/YYYY HH24:MI:SS';
   vExpected VARCHAR2(200) := 'DD/MM/YYYY HH24:MI:SS';
   vActual VARCHAR2(200);
BEGIN
   vActual := vInput;
   IF INSTR(vActual, '"T"') = 0 AND REGEXP_LIKE(vActual, '[YMD]T[HM]') THEN
      vActual := REGEXP_REPLACE(vActual, '([YMD])T([HM])', '\1"T"\2');
   END IF;
   
   DBMS_OUTPUT.PUT_LINE('Input:    ' || vInput);
   DBMS_OUTPUT.PUT_LINE('Expected: ' || vExpected);
   DBMS_OUTPUT.PUT_LINE('Actual:   ' || vActual);
   DBMS_OUTPUT.PUT_LINE('Result:   ' || CASE WHEN vActual = vExpected THEN 'PASS' ELSE 'FAIL' END);
END;
/

PROMPT
PROMPT Test Case 3: Already quoted T (no change)
PROMPT Input:  YYYY-MM-DD"T"HH24:MI:SS
PROMPT Expected: YYYY-MM-DD"T"HH24:MI:SS (unchanged)
PROMPT
DECLARE
   vInput VARCHAR2(200) := 'YYYY-MM-DD"T"HH24:MI:SS';
   vExpected VARCHAR2(200) := 'YYYY-MM-DD"T"HH24:MI:SS';
   vActual VARCHAR2(200);
BEGIN
   vActual := vInput;
   IF INSTR(vActual, '"T"') = 0 AND REGEXP_LIKE(vActual, '[YMD]T[HM]') THEN
      vActual := REGEXP_REPLACE(vActual, '([YMD])T([HM])', '\1"T"\2');
   END IF;
   
   DBMS_OUTPUT.PUT_LINE('Input:    ' || vInput);
   DBMS_OUTPUT.PUT_LINE('Expected: ' || vExpected);
   DBMS_OUTPUT.PUT_LINE('Actual:   ' || vActual);
   DBMS_OUTPUT.PUT_LINE('Result:   ' || CASE WHEN vActual = vExpected THEN 'PASS' ELSE 'FAIL' END);
END;
/

PROMPT
PROMPT ========================================
PROMPT Test Suite Complete
PROMPT ========================================
PROMPT
PROMPT To test with actual external table:
PROMPT 1. Configure date format: CALL FILE_MANAGER.ADD_COLUMN_DATE_FORMAT(...)
PROMPT 2. Create external table: CALL FILE_MANAGER.CREATE_EXTERNAL_TABLE(...)
PROMPT 3. Check DDL: SELECT DBMS_METADATA.GET_DDL('TABLE', 'EXT_TABLE', 'ODS') FROM DUAL;
PROMPT 4. Verify date format has quoted T: DATE 'YYYY-MM-DD"T"HH24:MI:SS...'
PROMPT ========================================
