"""
Alembic environment for programmatic migration execution.

This environment is designed for per-user encrypted SQLCipher databases.
It receives a connection via config.attributes set by alembic_runner.py.
"""

from alembic import context

# Import models metadata for autogenerate support
from local_deep_research.database.models import Base

# This is the Alembic Config object
config = context.config

# Model metadata for autogenerate
target_metadata = Base.metadata


def run_migrations_offline():
    """Run migrations in 'offline' mode - not supported in this project."""
    raise NotImplementedError(
        "Offline migrations not supported. "
        "Use alembic_runner.run_migrations() for programmatic execution."
    )


def run_migrations_online():
    """
    Run migrations in 'online' mode using the provided connection.

    The connection is passed via config.attributes["connection"] by alembic_runner.py,
    which opens it inside engine.begin() so that failures auto-rollback.
    """
    connection = config.attributes.get("connection")

    if connection is None:
        raise RuntimeError(
            "No connection provided via config.attributes['connection']. "
            "Use alembic_runner.run_migrations() for programmatic execution."
        )

    context.configure(
        connection=connection,
        target_metadata=target_metadata,
        render_as_batch=True,  # Required for SQLite ALTER TABLE
        compare_type=True,
    )

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


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