-- Migration 010 Verification Script
-- ==================================
-- This script verifies that migration 010_remove_polygon_configurations.sql
-- will run cleanly and produce the expected results.

-- Set error handling
\set ON_ERROR_STOP on
\set ECHO all

BEGIN;

-- === PRE-MIGRATION STATE CHECK ===

-- Check if provider tables exist (should exist from migration 009)
SELECT 
    CASE 
        WHEN EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'provider_configurations') 
        THEN 'PASS: provider_configurations table exists'
        ELSE 'FAIL: provider_configurations table missing - run migration 009 first'
    END as provider_config_check;

SELECT 
    CASE 
        WHEN EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'provider_subscriptions') 
        THEN 'PASS: provider_subscriptions table exists'
        ELSE 'FAIL: provider_subscriptions table missing - run migration 009 first'
    END as provider_subs_check;

SELECT 
    CASE 
        WHEN EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'provider_endpoints') 
        THEN 'PASS: provider_endpoints table exists'
        ELSE 'FAIL: provider_endpoints table missing - run migration 009 first'
    END as provider_endpoints_check;

-- Check if config tables exist (should exist from migration 007)
SELECT 
    CASE 
        WHEN EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'config_settings') 
        THEN 'PASS: config_settings table exists'
        ELSE 'FAIL: config_settings table missing - run migration 007 first'
    END as config_settings_check;

-- === SIMULATE MIGRATION EFFECTS ===

-- Count existing Polygon configurations (if any)
SELECT 
    COUNT(*) as polygon_configs_count,
    'Polygon configurations that will be removed' as description
FROM config_settings 
WHERE config_key ILIKE '%polygon%' 
   OR description ILIKE '%polygon%'
   OR category_path ILIKE '%polygon%';

-- Count existing Polygon categories (if any)
SELECT 
    COUNT(*) as polygon_categories_count,
    'Polygon categories that will be removed' as description
FROM config_categories 
WHERE category_name ILIKE '%polygon%' 
   OR description ILIKE '%polygon%'
   OR category_path ILIKE '%polygon%';

-- Count existing Databento configurations
SELECT 
    COUNT(*) as databento_configs_count,
    'Existing Databento configurations' as description
FROM config_settings 
WHERE category_path LIKE 'trading.providers.databento%';

-- Count existing Benzinga configurations
SELECT 
    COUNT(*) as benzinga_configs_count,
    'Existing Benzinga configurations' as description
FROM config_settings 
WHERE category_path LIKE 'trading.providers.benzinga%';

-- === VERIFY TABLE STRUCTURES ===

-- Check config_history table structure
SELECT 
    column_name, 
    data_type, 
    is_nullable
FROM information_schema.columns 
WHERE table_name = 'config_history'
ORDER BY ordinal_position
LIMIT 5;

-- Check if migration_notes category would conflict
SELECT 
    CASE 
        WHEN EXISTS (SELECT 1 FROM config_categories WHERE category_path = 'system.migration_notes') 
        THEN 'WARNING: migration_notes category already exists'
        ELSE 'OK: migration_notes category will be created'
    END as migration_notes_check;

-- === FUNCTION EXISTENCE CHECKS ===

-- Check if Polygon functions exist that will be dropped
SELECT 
    routine_name,
    'Function that will be dropped' as status
FROM information_schema.routines 
WHERE routine_name LIKE '%polygon%'
   OR routine_name IN ('get_polygon_config', 'set_polygon_config', 'notify_polygon_changes');

-- Check if tables that will be dropped exist
SELECT 
    table_name,
    'Table that will be dropped' as status
FROM information_schema.tables 
WHERE table_name IN ('polygon_configurations', 'polygon_subscriptions', 'polygon_endpoints', 'polygon_api_keys');

-- === FINAL VALIDATION ===

SELECT '=== MIGRATION 010 VERIFICATION COMPLETE ===' as summary;

SELECT 
    CASE 
        WHEN EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'provider_configurations')
         AND EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'config_settings')
        THEN 'READY: Migration 010 can be safely applied'
        ELSE 'NOT READY: Prerequisites missing - ensure migrations 007 and 009 are applied first'
    END as final_status;

ROLLBACK; -- Don't actually make any changes