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:
    # e.g. path = "plugins/plant/models.py"
    # We want to turn that into "plugins.plant.models"
    rel = path[len("plugins/") : -len("/models.py")]  # e.g. "plant"
    pkg = f"plugins.{rel}.models"                       # e.g. "plugins.plant.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")

# SQLAlchemy will look at this `target_metadata` when autogenerating
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"},
    )
    with context.begin_transaction():
        context.run_migrations()

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

def run_migrations_online():
    connectable = db.engine
    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()
