#!/usr/bin/env python3
"""Recreate all tables from models (fresh start)."""

import sys
import os

# Add project root to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from src.web.db.models import Base
from src.web.db.session import engine, get_database_url


def recreate_tables():
    """Recreate all tables from models."""
    db_url = get_database_url()
    print(f"Connecting to database: {db_url}")
    
    # Create all tables from models
    print("\nCreating all tables from models...")
    Base.metadata.create_all(bind=engine)
    
    print("✅ All tables created successfully!")
    
    # Stamp Alembic to latest migration
    print("\nMarking database as up-to-date with migrations...")
    from alembic.config import Config
    from alembic import command
    from alembic.script import ScriptDirectory
    
    alembic_cfg = Config("alembic.ini")
    script = ScriptDirectory.from_config(alembic_cfg)
    
    # Get the latest revision
    head = script.get_current_head()
    print(f"Stamping database to revision: {head}")
    command.stamp(alembic_cfg, head)
    
    print("✅ Database is now fresh and ready to use!")


if __name__ == "__main__":
    try:
        recreate_tables()
    except Exception as e:
        print(f"❌ Error recreating tables: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)

