#!/usr/bin/env python3
"""
Investigate MTG sets endpoint and image column.
Checks database schema, MTGJSON data structure, and API responses.
"""

import asyncio
import os
import sys
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker
from sqlalchemy import text

# Add project root to path
sys.path.insert(0, '/home/wall-o/projects/mtgonline/backend')

from app.core.settings import get_settings
from app.models.mtg_models import MtgSet, MtgCard


async def main():
    """Investigate the current state."""
    settings = get_settings()
    
    print("=== DATABASE CONNECTION ===")
    print(f"MTG DB URL: {settings.MTG_DATABASE_URL}")
    print()
    
    engine = create_async_engine(settings.MTG_DATABASE_URL)
    async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
    
    async with async_session() as session:
        # Check mtg_sets table schema
        print("=== MTG_SETS TABLE SCHEMA ===")
        result = await session.execute(text("""
            SELECT column_name, data_type, is_nullable
            FROM information_schema.columns 
            WHERE table_name = 'mtg_sets' 
            ORDER BY ordinal_position
        """))
        for row in result.fetchall():
            print(f"  {row[0]}: {row[1]} (nullable: {row[2]})")
        
        print()
        print("=== SAMPLE SET DATA ===")
        result = await session.execute(text("""
            SELECT code, name, type, release_date, base_set_size, total_size,
                   is_foil_only, is_non_foil_only, digital, icon_svg_url,
                   parent_code, mtgo_code
            FROM mtg_sets 
            LIMIT 1
        """))
        row = result.fetchone()
        if row:
            cols = ['code', 'name', 'type', 'release_date', 'base_set_size', 'total_size',
                    'is_foil_only', 'is_non_foil_only', 'digital', 'icon_svg_url',
                    'parent_code', 'mtgo_code']
            for col, val in zip(cols, row):
                print(f"  {col}: {val}")
        
        print()
        print("=== SET COUNT ===")
        result = await session.execute(text("SELECT COUNT(*) FROM mtg_sets"))
        count = result.scalar()
        print(f"  Total sets: {count}")
        
        print()
        print("=== IMAGE URL CHECK ===")
        result = await session.execute(text("""
            SELECT COUNT(*) FROM mtg_sets 
            WHERE image_url IS NOT NULL AND image_url != ''
        """))
        count = result.scalar()
        print(f"  Sets with image_url: {count}")
        
        print()
        print("=== CHECKING FOR image_url COLUMN ===")
        result = await session.execute(text("""
            SELECT column_name FROM information_schema.columns 
            WHERE table_name = 'mtg_sets' AND column_name LIKE '%image%'
        """))
        image_cols = [row[0] for row in result.fetchall()]
        print(f"  Image-related columns: {image_cols}")
    
    await engine.dispose()
    
    # Check MTGJSON data structure
    print()
    print("=== MTGJSON SET SCHEMA REFERENCE ===")
    print("MTGJSON set.json fields related to images:")
    print("  - image: Object with 'normal' and 'large' URLs")
    print("  - image_png: URL to PNG image")
    print("  - image_png_small: URL to small PNG image")
    print("  - icon_svg_url: SVG icon URL")
    print("  - symbol: Symbol image URL")
    print("  - logo: Logo image URL")
    
    print()
    print("=== CONCLUSION ===")
    print("The mtg_sets table needs an image_url column to store")
    print("the normal-sized image URL from MTGJSON set data.")
    print()
    print("Steps needed:")
    print("1. Add image_url column to mtg_sets table")
    print("2. Update MtgSet model")
    print("3. Update refresh_mtg.py to fetch image_url from set.json")
    print("4. Update get_sets() and get_set_by_code() to return image_url")


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