import asyncio
from sqlalchemy.ext.asyncio import create_async_engine
import sys
from pathlib import Path

# Add the parent directory to sys.path to allow absolute imports
sys.path.append(str(Path(__file__).parent.parent))

from database.models import Base
from database.base import SQLALCHEMY_DATABASE_URL
from dotenv import load_dotenv

load_dotenv()

async def init_mysql():
    """Initialize MySQL database and create all tables"""
    # Create async engine
    engine = create_async_engine(
        SQLALCHEMY_DATABASE_URL,
        echo=True  # Enable logging for table creation
    )

    try:
        print("Creating tables...")
        async with engine.begin() as conn:
            # Create all tables
            await conn.run_sync(Base.metadata.create_all)
        print("Tables created successfully!")
    except Exception as e:
        print(f"Error creating tables: {e}")
        raise
    finally:
        await engine.dispose()

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