# alembic/env.py
from logging.config import fileConfig

from sqlalchemy import engine_from_config
from sqlalchemy import pool
from sqlalchemy.engine import Connection
from sqlalchemy.ext.asyncio import create_async_engine, AsyncEngine
from alembic import context

import os
import sys

# Add the project root directory to the Python path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
# Import your settings and models
from src.config import settings
from src.infrastructure.persistence.models.base import Base
from src.infrastructure.persistence.models.chart_model import ChartImageModel
from src.infrastructure.persistence.models.conversation_models import (
    ConversationModel, 
    ConversationMessageModel, 
    ConversationChartReferenceModel
)
from src.infrastructure.persistence.models.user_model import UserModel
from src.infrastructure.persistence.models.analysis_model import ChartAnalysisModel
from src.infrastructure.persistence.models.blacklisted_token_model import BlacklistedTokenModel

# ------------------------------
# Configure Alembic
# ------------------------------
config = context.config

# Setup logging
if config.config_file_name is not None:
    fileConfig(config.config_file_name)

# Set target metadata for autogenerate
target_metadata = Base.metadata

# Set the database URL from settings
config.set_main_option('sqlalchemy.url', settings.DATABASE_URL)
url = config.get_main_option("sqlalchemy.url")

# ------------------------------
# Offline Mode
# ------------------------------
def run_migrations_offline() -> None:
    """Run migrations in 'offline' mode."""
    context.configure(
        url=url,
        target_metadata=target_metadata,
        literal_binds=True,
        dialect_opts={"paramstyle": "named"},
    )

    with context.begin_transaction():
        context.run_migrations()


# ------------------------------
# Online Mode (with sync engine)
# ------------------------------
def run_migrations_online() -> None:
    """Run migrations in 'online' mode."""
    connectable = engine_from_config(
        config.get_section(config.config_ini_section, {}),
        prefix="sqlalchemy.",
        poolclass=pool.NullPool,
    )

    with connectable.connect() as connection:
        context.configure(
            connection=connection, target_metadata=target_metadata
        )

        with context.begin_transaction():
            context.run_migrations()


# ------------------------------
# Run the appropriate mode
# ------------------------------
if context.is_offline_mode():
    run_migrations_offline()
else:
    run_migrations_online()