"""
Simple migration runner. Runs schema.sql against the database.
"""
import sqlite3
from pathlib import Path
import os
import sys

# Add parent to path for imports
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent))

from dotenv import load_dotenv

load_dotenv()


def migrate():
    """Run migrations."""
    # Get database path from env or default
    db_path = os.getenv("DATABASE_PATH", "data/app.db")
    
    # Ensure directory exists
    Path(db_path).parent.mkdir(parents=True, exist_ok=True)
    
    # Read schema
    schema_path = Path(__file__).parent / "schema.sql"
    schema = schema_path.read_text()
    
    # Connect and execute
    conn = sqlite3.connect(db_path)
    
    # Enable WAL mode
    conn.execute("PRAGMA journal_mode=WAL")
    conn.execute("PRAGMA foreign_keys=ON")
    
    # Run schema
    conn.executescript(schema)
    conn.commit()
    
    print(f"✓ Migrations complete: {db_path}")
    
    # Show tables
    cursor = conn.execute(
        "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name"
    )
    tables = [row[0] for row in cursor.fetchall()]
    print(f"  Tables: {', '.join(tables)}")
    
    conn.close()


if __name__ == "__main__":
    migrate()
