-- Comprehensive check for all missing columns
-- Run: psql -h 10.9.23.11 -p 54321 -U postgres -d loaf_new -f check_all_columns.sql

\echo '================================================================'
\echo 'COMPREHENSIVE COLUMN CHECK FOR ALL TABLES'
\echo '================================================================'

-- ============================================================
-- 1. USERS TABLE
-- ============================================================
\echo ''
\echo '1. USERS TABLE - Expected: 60+ columns'
\echo 'Checking for specific columns:'

SELECT
    CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'users' AND column_name = 'newsletter_publish_name') THEN '✓' ELSE '✗' END || ' newsletter_publish_name',
    CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'users' AND column_name = 'volunteer_interests') THEN '✓' ELSE '✗' END || ' volunteer_interests',
    CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'users' AND column_name = 'scholarship_requested') THEN '✓' ELSE '✗' END || ' scholarship_requested',
    CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'users' AND column_name = 'show_in_directory') THEN '✓' ELSE '✗' END || ' show_in_directory',
    CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'users' AND column_name = 'password_reset_token') THEN '✓' ELSE '✗' END || ' password_reset_token',
    CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'users' AND column_name = 'accepts_tos') THEN '✓' ELSE '✗' END || ' accepts_tos',
    CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'users' AND column_name = 'member_since') THEN '✓' ELSE '✗' END || ' member_since',
    CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'users' AND column_name = 'rejection_reason') THEN '✓' ELSE '✗' END || ' rejection_reason',
    CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'users' AND column_name = 'import_source') THEN '✓' ELSE '✗' END || ' import_source'
\gx

-- ============================================================
-- 2. EVENTS TABLE
-- ============================================================
\echo ''
\echo '2. EVENTS TABLE'

SELECT
    CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'events' AND column_name = 'calendar_uid') THEN '✓' ELSE '✗' END || ' calendar_uid';

-- ============================================================
-- 3. SUBSCRIPTIONS TABLE
-- ============================================================
\echo ''
\echo '3. SUBSCRIPTIONS TABLE'

SELECT
    CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'subscriptions' AND column_name = 'base_subscription_cents') THEN '✓' ELSE '✗' END || ' base_subscription_cents',
    CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'subscriptions' AND column_name = 'donation_cents') THEN '✓' ELSE '✗' END || ' donation_cents',
    CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'subscriptions' AND column_name = 'manual_payment') THEN '✓' ELSE '✗' END || ' manual_payment'
\gx

-- ============================================================
-- 4. IMPORT_JOBS TABLE
-- ============================================================
\echo ''
\echo '4. IMPORT_JOBS TABLE'

SELECT
    CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'import_jobs' AND column_name = 'field_mapping') THEN '✓' ELSE '✗' END || ' field_mapping',
    CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'import_jobs' AND column_name = 'wordpress_metadata') THEN '✓' ELSE '✗' END || ' wordpress_metadata',
    CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'import_jobs' AND column_name = 'imported_user_ids') THEN '✓' ELSE '✗' END || ' imported_user_ids',
    CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'import_jobs' AND column_name = 'rollback_at') THEN '✓' ELSE '✗' END || ' rollback_at',
    CASE WHEN EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'import_jobs' AND column_name = 'rollback_by') THEN '✓' ELSE '✗' END || ' rollback_by'
\gx

-- ============================================================
-- 5. CHECK IF IMPORT_ROLLBACK_AUDIT TABLE EXISTS
-- ============================================================
\echo ''
\echo '5. IMPORT_ROLLBACK_AUDIT TABLE - Should exist'
SELECT CASE
    WHEN EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'import_rollback_audit')
    THEN '✓ Table exists'
    ELSE '✗ TABLE MISSING - Need to create it'
END AS status;

-- ============================================================
-- SUMMARY: Count existing columns in each table
-- ============================================================
\echo ''
\echo '================================================================'
\echo 'SUMMARY: Column counts per table'
\echo '================================================================'

SELECT
    table_name,
    COUNT(*) as column_count
FROM information_schema.columns
WHERE table_name IN (
    'users', 'events', 'event_rsvps', 'subscription_plans', 'subscriptions',
    'donations', 'event_galleries', 'newsletter_archives', 'financial_reports',
    'bylaws_documents', 'storage_usage', 'permissions', 'roles', 'role_permissions',
    'user_invitations', 'import_jobs', 'import_rollback_audit'
)
GROUP BY table_name
ORDER BY table_name;
