--=============================================================================================================================
-- MARS-1049-ADHOC: Verify CSDB External Tables Creation
--=============================================================================================================================
-- Purpose: Verify all 18 CSDB external tables exist with correct configuration
-- Author: Grzegorz Michalski
-- Date: 2025-11-26
-- Related: MARS-1049 (CSV Encoding Support), MARS-821 (Original CSDB Setup)
--
-- Expected Tables (18 total):
--   CSDB_DEBT: INBOX, ODS, ARCHIVE
--   CSDB_DEBT_DAILY: INBOX, ODS, ARCHIVE
--   CSDB_INSTR_DESC_FULL: INBOX, ODS, ARCHIVE
--   CSDB_INSTR_RAT_FULL: INBOX, ODS, ARCHIVE
--   CSDB_ISSUER_DESC_FULL: INBOX, ODS, ARCHIVE
--   CSDB_ISSUER_RAT_FULL: INBOX, ODS, ARCHIVE
--=============================================================================================================================

SET LINESIZE 200
SET PAGESIZE 100
SET SERVEROUTPUT ON

PROMPT =========================================================================
PROMPT MARS-1049-ADHOC: External Tables Verification Report
PROMPT =========================================================================
PROMPT 

PROMPT ========================================
PROMPT Step 1: Check External Tables Existence
PROMPT ========================================

SELECT 
    owner,
    table_name,
    CASE 
        WHEN table_name LIKE '%_INBOX' THEN 'INBOX'
        WHEN table_name LIKE '%_ODS' THEN 'ODS'
        WHEN table_name LIKE '%_ARCHIVE' THEN 'ARCHIVE'
    END AS bucket_area,
    default_directory_name AS directory,
    type_name
FROM all_external_tables
WHERE owner = 'ODS'
  AND table_name IN (
    'CSDB_DEBT_ARCHIVE',
    'CSDB_DEBT_INBOX',
    'CSDB_DEBT_ODS',
    'CSDB_DEBT_DAILY_ARCHIVE',
    'CSDB_DEBT_DAILY_INBOX',
    'CSDB_DEBT_DAILY_ODS',
    'CSDB_INSTR_DESC_FULL_ARCHIVE',
    'CSDB_INSTR_DESC_FULL_INBOX',
    'CSDB_INSTR_DESC_FULL_ODS',
    'CSDB_INSTR_RAT_FULL_ARCHIVE',
    'CSDB_INSTR_RAT_FULL_INBOX',
    'CSDB_INSTR_RAT_FULL_ODS',
    'CSDB_ISSUER_DESC_FULL_ARCHIVE',
    'CSDB_ISSUER_DESC_FULL_INBOX',
    'CSDB_ISSUER_DESC_FULL_ODS',
    'CSDB_ISSUER_RAT_FULL_ARCHIVE',
    'CSDB_ISSUER_RAT_FULL_INBOX',
    'CSDB_ISSUER_RAT_FULL_ODS'
)
ORDER BY table_name;

PROMPT 
PROMPT ========================================
PROMPT Step 2: Verify Table Counts by Category
PROMPT ========================================

SELECT 
    CASE 
        WHEN table_name LIKE 'CSDB_DEBT_DAILY%' THEN 'CSDB_DEBT_DAILY'
        WHEN table_name LIKE 'CSDB_DEBT%' THEN 'CSDB_DEBT'
        WHEN table_name LIKE 'CSDB_INSTR_DESC_FULL%' THEN 'CSDB_INSTR_DESC_FULL'
        WHEN table_name LIKE 'CSDB_INSTR_RAT_FULL%' THEN 'CSDB_INSTR_RAT_FULL'
        WHEN table_name LIKE 'CSDB_ISSUER_DESC_FULL%' THEN 'CSDB_ISSUER_DESC_FULL'
        WHEN table_name LIKE 'CSDB_ISSUER_RAT_FULL%' THEN 'CSDB_ISSUER_RAT_FULL'
    END AS table_category,
    COUNT(*) AS table_count,
    CASE WHEN COUNT(*) = 3 THEN 'COMPLETE' ELSE 'INCOMPLETE' END AS status
FROM all_external_tables
WHERE owner = 'ODS'
  AND table_name IN (
    'CSDB_DEBT_ARCHIVE', 'CSDB_DEBT_INBOX', 'CSDB_DEBT_ODS',
    'CSDB_DEBT_DAILY_ARCHIVE', 'CSDB_DEBT_DAILY_INBOX', 'CSDB_DEBT_DAILY_ODS',
    'CSDB_INSTR_DESC_FULL_ARCHIVE', 'CSDB_INSTR_DESC_FULL_INBOX', 'CSDB_INSTR_DESC_FULL_ODS',
    'CSDB_INSTR_RAT_FULL_ARCHIVE', 'CSDB_INSTR_RAT_FULL_INBOX', 'CSDB_INSTR_RAT_FULL_ODS',
    'CSDB_ISSUER_DESC_FULL_ARCHIVE', 'CSDB_ISSUER_DESC_FULL_INBOX', 'CSDB_ISSUER_DESC_FULL_ODS',
    'CSDB_ISSUER_RAT_FULL_ARCHIVE', 'CSDB_ISSUER_RAT_FULL_INBOX', 'CSDB_ISSUER_RAT_FULL_ODS'
)
GROUP BY 
    CASE 
        WHEN table_name LIKE 'CSDB_DEBT_DAILY%' THEN 'CSDB_DEBT_DAILY'
        WHEN table_name LIKE 'CSDB_DEBT%' THEN 'CSDB_DEBT'
        WHEN table_name LIKE 'CSDB_INSTR_DESC_FULL%' THEN 'CSDB_INSTR_DESC_FULL'
        WHEN table_name LIKE 'CSDB_INSTR_RAT_FULL%' THEN 'CSDB_INSTR_RAT_FULL'
        WHEN table_name LIKE 'CSDB_ISSUER_DESC_FULL%' THEN 'CSDB_ISSUER_DESC_FULL'
        WHEN table_name LIKE 'CSDB_ISSUER_RAT_FULL%' THEN 'CSDB_ISSUER_RAT_FULL'
    END
ORDER BY table_category;

PROMPT 
PROMPT ========================================
PROMPT Step 3: Overall Summary
PROMPT ========================================

DECLARE
    v_total_count NUMBER;
    v_expected_count NUMBER := 18;
    v_status VARCHAR2(20);
BEGIN
    SELECT COUNT(*) INTO v_total_count
    FROM all_external_tables
    WHERE owner = 'ODS'
      AND table_name IN (
        'CSDB_DEBT_ARCHIVE', 'CSDB_DEBT_INBOX', 'CSDB_DEBT_ODS',
        'CSDB_DEBT_DAILY_ARCHIVE', 'CSDB_DEBT_DAILY_INBOX', 'CSDB_DEBT_DAILY_ODS',
        'CSDB_INSTR_DESC_FULL_ARCHIVE', 'CSDB_INSTR_DESC_FULL_INBOX', 'CSDB_INSTR_DESC_FULL_ODS',
        'CSDB_INSTR_RAT_FULL_ARCHIVE', 'CSDB_INSTR_RAT_FULL_INBOX', 'CSDB_INSTR_RAT_FULL_ODS',
        'CSDB_ISSUER_DESC_FULL_ARCHIVE', 'CSDB_ISSUER_DESC_FULL_INBOX', 'CSDB_ISSUER_DESC_FULL_ODS',
        'CSDB_ISSUER_RAT_FULL_ARCHIVE', 'CSDB_ISSUER_RAT_FULL_INBOX', 'CSDB_ISSUER_RAT_FULL_ODS'
    );
    
    IF v_total_count = v_expected_count THEN
        v_status := 'SUCCESS';
    ELSE
        v_status := 'FAILED';
    END IF;
    
    DBMS_OUTPUT.PUT_LINE('=========================================================================');
    DBMS_OUTPUT.PUT_LINE('VERIFICATION STATUS: ' || v_status);
    DBMS_OUTPUT.PUT_LINE('=========================================================================');
    DBMS_OUTPUT.PUT_LINE('Expected Tables: ' || v_expected_count);
    DBMS_OUTPUT.PUT_LINE('Found Tables:    ' || v_total_count);
    DBMS_OUTPUT.PUT_LINE('Missing Tables:  ' || (v_expected_count - v_total_count));
    DBMS_OUTPUT.PUT_LINE('=========================================================================');
    
    IF v_total_count = v_expected_count THEN
        DBMS_OUTPUT.PUT_LINE('');
        DBMS_OUTPUT.PUT_LINE('All 18 CSDB external tables created successfully with WE8MSWIN1252 encoding');
        DBMS_OUTPUT.PUT_LINE('MARS-1049-ADHOC deployment completed successfully');
    ELSE
        DBMS_OUTPUT.PUT_LINE('');
        DBMS_OUTPUT.PUT_LINE('WARNING: Not all tables were created. Review errors from step 02.');
        RAISE_APPLICATION_ERROR(-20001, 'External tables verification failed');
    END IF;
END;
/

PROMPT 
PROMPT ========================================
PROMPT Step 4: Check Missing Tables (if any)
PROMPT ========================================

WITH expected_tables AS (
    SELECT 'CSDB_DEBT_ARCHIVE' AS table_name FROM DUAL UNION ALL
    SELECT 'CSDB_DEBT_INBOX' FROM DUAL UNION ALL
    SELECT 'CSDB_DEBT_ODS' FROM DUAL UNION ALL
    SELECT 'CSDB_DEBT_DAILY_ARCHIVE' FROM DUAL UNION ALL
    SELECT 'CSDB_DEBT_DAILY_INBOX' FROM DUAL UNION ALL
    SELECT 'CSDB_DEBT_DAILY_ODS' FROM DUAL UNION ALL
    SELECT 'CSDB_INSTR_DESC_FULL_ARCHIVE' FROM DUAL UNION ALL
    SELECT 'CSDB_INSTR_DESC_FULL_INBOX' FROM DUAL UNION ALL
    SELECT 'CSDB_INSTR_DESC_FULL_ODS' FROM DUAL UNION ALL
    SELECT 'CSDB_INSTR_RAT_FULL_ARCHIVE' FROM DUAL UNION ALL
    SELECT 'CSDB_INSTR_RAT_FULL_INBOX' FROM DUAL UNION ALL
    SELECT 'CSDB_INSTR_RAT_FULL_ODS' FROM DUAL UNION ALL
    SELECT 'CSDB_ISSUER_DESC_FULL_ARCHIVE' FROM DUAL UNION ALL
    SELECT 'CSDB_ISSUER_DESC_FULL_INBOX' FROM DUAL UNION ALL
    SELECT 'CSDB_ISSUER_DESC_FULL_ODS' FROM DUAL UNION ALL
    SELECT 'CSDB_ISSUER_RAT_FULL_ARCHIVE' FROM DUAL UNION ALL
    SELECT 'CSDB_ISSUER_RAT_FULL_INBOX' FROM DUAL UNION ALL
    SELECT 'CSDB_ISSUER_RAT_FULL_ODS' FROM DUAL
)
SELECT e.table_name AS missing_table
FROM expected_tables e
LEFT JOIN all_external_tables t ON e.table_name = t.table_name AND t.owner = 'ODS'
WHERE t.table_name IS NULL
ORDER BY e.table_name;

PROMPT 
PROMPT =========================================================================
PROMPT Verification Report Complete
PROMPT =========================================================================
