-- ============================================================================
-- Database Diagnostic Script
-- Run this to check what exists in your database
-- ============================================================================

\echo '=== CHECKING ENUMS ==='
SELECT
    t.typname as enum_name,
    string_agg(e.enumlabel, ', ' ORDER BY e.enumsortorder) as values
FROM pg_type t
JOIN pg_enum e ON t.oid = e.enumtypid
WHERE t.typname IN (
    'userstatus', 'userrole', 'rsvpstatus', 'subscriptionstatus',
    'donationtype', 'donationstatus', 'invitationstatus', 'importjobstatus'
)
GROUP BY t.typname
ORDER BY t.typname;

\echo ''
\echo '=== CHECKING TABLES ==='
SELECT
    schemaname,
    tablename
FROM pg_tables
WHERE schemaname = 'public'
ORDER BY tablename;

\echo ''
\echo '=== CHECKING USERS TABLE STRUCTURE ==='
SELECT
    column_name,
    data_type,
    is_nullable,
    column_default
FROM information_schema.columns
WHERE table_name = 'users'
ORDER BY ordinal_position;

\echo ''
\echo '=== CHECKING FOR CRITICAL FIELDS ==='
\echo 'Checking if reminder tracking fields exist...'
SELECT EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_name = 'users'
    AND column_name = 'email_verification_reminders_sent'
) as has_reminder_fields;

\echo ''
\echo 'Checking if accepts_tos field exists (should be accepts_tos, not tos_accepted)...'
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'users'
AND column_name IN ('accepts_tos', 'tos_accepted');

\echo ''
\echo 'Checking if WordPress import fields exist...'
SELECT EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_name = 'users'
    AND column_name = 'import_source'
) as has_import_fields;

\echo ''
\echo '=== CHECKING IMPORT_JOBS TABLE ==='
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'import_jobs'
ORDER BY ordinal_position;

\echo ''
\echo '=== SUMMARY ==='
SELECT
    (SELECT COUNT(*) FROM pg_type WHERE typname IN (
        'userstatus', 'userrole', 'rsvpstatus', 'subscriptionstatus',
        'donationtype', 'donationstatus', 'invitationstatus', 'importjobstatus'
    )) as enum_count,
    (SELECT COUNT(*) FROM pg_tables WHERE schemaname = 'public') as table_count,
    (SELECT COUNT(*) FROM information_schema.columns WHERE table_name = 'users') as users_column_count;
