from __future__ import with_statement
import os
import logging
import glob
import importlib

from alembic import context
from sqlalchemy import engine_from_config, pool
from logging.config import fileConfig

from flask import current_app
from app import db

# -----------------------------
# 🔍 Automatically import all plugin models under their real package name
# -----------------------------
plugin_model_paths = glob.glob(os.path.join("plugins", "*", "models.py"))

for path in plugin_model_paths:
    rel = path[len("plugins/") : -len("/models.py")]
    pkg = f"plugins.{rel}.models"
    try:
        importlib.import_module(pkg)
        print(f"✅ Loaded: {pkg}")
    except Exception as e:
        print(f"❌ Failed to load {pkg}: {e}")
# -----------------------------

config = context.config
fileConfig(config.config_file_name)
logger = logging.getLogger("alembic.env")
logger.setLevel(logging.WARN)  # optional: silence alembic spam

target_metadata = db.metadata

def run_migrations_offline():
    context.configure(
        url=current_app.config.get("SQLALCHEMY_DATABASE_URI"),
        target_metadata=target_metadata,
        literal_binds=True,
        dialect_opts={"paramstyle": "named"},
        sort_tables=True,
        render_as_batch=True,  # ✅ important!
    )
    with context.begin_transaction():
        context.run_migrations()

def run_migrations_online():
    connectable = db.engine
    with connectable.connect() as connection:
        context.configure(
            connection=connection,
            target_metadata=target_metadata,
            compare_type=True,
            sort_tables=True,
            render_as_batch=True,  # ✅ important!
        )
        with context.begin_transaction():
            context.run_migrations()

print("🧠 Alembic sees these tables:")
print(sorted(db.metadata.tables.keys()))

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