import os
import sys
from logging.config import fileConfig

from sqlalchemy import engine_from_config, pool
from alembic import context

# ── Add project root to path so we can import config.py and models ────────────
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

# ── Import app settings and all ORM models ────────────────────────────────────
from config import get_settings
from database import Base, DATABASE_URL  # DATABASE_URL here is already resolved to an
                                          # absolute path for sqlite — see database.py.
                                          # Using the raw settings.DATABASE_URL here would
                                          # let alembic target a DIFFERENT file than the
                                          # running app if invoked from a different cwd.
from models.orm import (
    User, AuditLog, Supplier, Product, Inventory,
    Customer, Sale, SaleItem,
    PurchaseOrder, POItem, ProductAlert, MpesaTransaction,
    PasswordResetCode,
)

settings = get_settings()

# ── Standard Alembic boilerplate ──────────────────────────────────────────────
config = context.config

if config.config_file_name is not None:
    fileConfig(config.config_file_name)

# ── Inject DATABASE_URL from .env into Alembic ────────────────────────────────
db_url = DATABASE_URL
if db_url.startswith("postgres://"):
    db_url = db_url.replace("postgres://", "postgresql://", 1)

config.set_main_option("sqlalchemy.url", db_url)

target_metadata = Base.metadata


# ── Offline mode (generates SQL file without connecting to DB) ────────────────
def run_migrations_offline() -> None:
    url = config.get_main_option("sqlalchemy.url")
    context.configure(
        url=url,
        target_metadata=target_metadata,
        literal_binds=True,
        dialect_opts={"paramstyle": "named"},
        compare_type=True,
    )
    with context.begin_transaction():
        context.run_migrations()


# ── Online mode (connects to real DB and runs migrations) ─────────────────────
def run_migrations_online() -> None:
    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,
            compare_type=True,
        )
        with context.begin_transaction():
            context.run_migrations()


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