#!/usr/bin/env python3
"""Check MTGJSON data status in the database and filesystem."""

import asyncio
import os
from pathlib import Path
from sqlalchemy import create_engine, text
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
import sys
sys.path.append("/home/wall-o/projects/mtgonline/backend")

from app.services.mtgjson_manager import MTGJSONManager
from app.config import get_settings

async def check_mtgjson_status():
    """Check MTGJSON data download and database status."""
    
    print("=== MTGJSON Data Status Check ===\n")
    
    # Check data directory
    settings = get_settings()
    data_dir = Path(settings.DATA_DIR)
    
    print(f"Data Directory: {data_dir}")
    print(f"Directory exists: {data_dir.exists()}")
    
    if data_dir.exists():
        files = list(data_dir.glob("*.json.gz"))
        files += list(data_dir.glob("*.json"))
        print(f"Found {len(files)} MTGJSON files:")
        for f in sorted(files)[:20]:  # Show first 20
            size_mb = f.stat().st_size / (1024 * 1024)
            print(f"  - {f.name} ({size_mb:.1f} MB)")
        if len(files) > 20:
            print(f"  ... and {len(files) - 20} more files")
    else:
        print("WARNING: Data directory does not exist!")
    
    # Check database status
    print("\n=== Database Status ===")
    
    try:
        # Try to connect to the MTGJSON database
        engine = create_async_engine(
            f"postgresql+asyncpg://{settings.POSTGRES_USER}:{settings.POSTGRES_PASSWORD}"
            f"@postgres-mtgdata:5432/{settings.POSTGRES_DB}"
        )
        
        async with AsyncSession(engine) as session:
            # Check if tables exist
            result = await session.execute(text("""
                SELECT table_name 
                FROM information_schema.tables 
                WHERE table_schema = 'public'
                ORDER BY table_name;
            """))
            
            tables = [row[0] for row in result.fetchall()]
            print(f"Found {len(tables)} tables in database:")
            for table in tables:
                print(f"  - {table}")
            
            # Check specific MTGJSON tables
            mtg_tables = ['mtg_set', 'mtg_card', 'mtg_identifiers', 'mtg_keywords']
            if 'mtg_refresh_log' in tables:
                result = await session.execute(text("SELECT COUNT(*) FROM mtg_refresh_log"))
                count = result.scalar()
                print(f"\nRefresh log entries: {count}")
            
            # Check key tables
            for table in ['mtg_set', 'mtg_card', 'mtg_identifiers']:
                if table in tables:
                    result = await session.execute(text(f"SELECT COUNT(*) FROM {table}"))
                    count = result.scalar()
                    print(f"{table}: {count:,} records")
                
    except Exception as e:
        print(f"ERROR connecting to database: {e}")
    
    # Try to create MTGJSON manager and check status
    print("\n=== MTGJSON Manager Status ===")
    try:
        manager = MTGJSONManager()
        status = manager.get_status()
        print(f"Status: {status}")
    except Exception as e:
        print(f"ERROR creating MTGJSON manager: {e}")

if __name__ == "__main__":
    asyncio.run(check_mtgjson_status())
