from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
import os
import sys

# Add the src directory to Python path so we can import our models
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))

# Import settings to get database URL
from src.core.config import settings

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Set the database URL from settings
config.set_main_option("sqlalchemy.url", settings.getDbConnectionUri())

# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
    fileConfig(config.config_file_name)

# add your model's MetaData object here
# for 'autogenerate' support
from src.apps.base.models.base import Base

# Import all models to ensure SQLAlchemy can resolve relationships
from src.apps.users.models.user import User
from src.apps.auth.models.auth_session import AuthSession
from src.apps.payment_requests.models import *
from src.apps.merchants.models import *
from src.apps.base.models import *  # Import the model from common.models
from src.apps.invoices.models import *  # Import invoice models
from src.apps.customers.models import *  # Import customer models
from src.apps.products.models import *  # Import product models
from src.apps.product_categories.models import *  # Import product category models
from src.apps.files.models import *  # Import file models
from src.apps.notes.models import *  # Import note models
from src.apps.receipts.models import *  # Import receipt models
from src.apps.transactions.models import *  # Import transaction models
from src.apps.site_settings.models import *  # Import settings models
from src.apps.site_masters.models import *  # Import settings models
from src.apps.role_permissions.models import *  # Import settings models

target_metadata = Base.metadata


def run_migrations_offline() -> None:
    """Run migrations in 'offline' mode.

    This configures the context with just a URL
    and not an Engine, though an Engine is acceptable
    here as well.  By skipping the Engine creation
    we don't even need a DBAPI to be available.

    Calls to context.execute() here emit the given string to the
    script output.

    """
    url = config.get_main_option("sqlalchemy.url")
    context.configure(
        url=url,
        target_metadata=target_metadata,
        literal_binds=True,
        dialect_opts={"paramstyle": "named"},
    )

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


def run_migrations_online() -> None:
    """Run migrations in 'online' mode.

    In this scenario we need to create an Engine
    and associate a connection with the context.

    """
    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()


if context.is_offline_mode():
    run_migrations_offline()
else:
    run_migrations_online()
