--=============================================================================================================================
-- MARS-1049-ADHOC: Update CSDB Encoding Configuration
--=============================================================================================================================
-- Purpose: Change encoding for CSDB configurations from UTF8 to WE8MSWIN1252
-- Author: Grzegorz Michalski
-- Date: 2025-11-26
-- Related: MARS-1049 (CSV Encoding Support), MARS-821 (Original CSDB Setup)
--
-- Why WE8MSWIN1252 for CSDB:
-- - CSDB files contain international characters (Turkish: Türkiye, etc.)
-- - Source system uses Windows-1252 encoding
-- - Proper character handling requires matching encoding
--=============================================================================================================================

SET SERVEROUTPUT ON

PROMPT =========================================================================
PROMPT MARS-1049-ADHOC: Update CSDB Encoding Configuration
PROMPT =========================================================================

-- Step 1: Show current state
PROMPT 
PROMPT Step 1: Current CSDB encoding configuration
PROMPT ========================================

SELECT 
    TABLE_ID,
    SOURCE_FILE_ID,
    ENCODING AS CURRENT_ENCODING,
    TEMPLATE_TABLE_NAME
FROM CT_MRDS.A_SOURCE_FILE_CONFIG
WHERE TEMPLATE_TABLE_NAME LIKE '%CSDB%'
ORDER BY TABLE_ID, SOURCE_FILE_ID;

PROMPT 
PROMPT Step 2: Update CSDB configurations to WE8MSWIN1252
PROMPT ========================================

-- Update all CSDB configurations to Windows-1252 encoding
UPDATE /*+ NOPARALLEL */ CT_MRDS.A_SOURCE_FILE_CONFIG 
SET ENCODING = 'WE8MSWIN1252'
WHERE TEMPLATE_TABLE_NAME LIKE '%CSDB%';

PROMPT Updated CSDB configurations with WE8MSWIN1252 encoding

-- Commit changes
COMMIT;

PROMPT 
PROMPT Step 3: Verify updated encoding configuration
PROMPT ========================================

SELECT 
    TABLE_ID,
    SOURCE_FILE_ID,
    ENCODING AS NEW_ENCODING,
    TEMPLATE_TABLE_NAME
FROM CT_MRDS.A_SOURCE_FILE_CONFIG
WHERE TEMPLATE_TABLE_NAME LIKE '%CSDB%'
ORDER BY TABLE_ID, SOURCE_FILE_ID;

PROMPT 
PROMPT Step 4: Overall encoding distribution
PROMPT ========================================

SELECT 
    ENCODING,
    COUNT(*) AS CONFIG_COUNT,
    LISTAGG(DISTINCT TABLE_ID, ', ') WITHIN GROUP (ORDER BY TABLE_ID) AS TABLES
FROM CT_MRDS.A_SOURCE_FILE_CONFIG
GROUP BY ENCODING
ORDER BY ENCODING;

PROMPT 
PROMPT =========================================================================
PROMPT CSDB Encoding Update - COMPLETED
PROMPT =========================================================================
PROMPT 
PROMPT Summary:
PROMPT - All CSDB configurations updated to WE8MSWIN1252
PROMPT - Encoding matches source system character set (Windows-1252)
PROMPT - Proper handling of international characters enabled
PROMPT 
PROMPT Next Step: Run 01_MARS_1049_ADHOC_drop_external_tables.sql
PROMPT =========================================================================
