"""Initial schema - baseline migration

This migration creates all tables from the current models.
For existing databases (pre-Alembic), this migration is skipped
via stamping - the database is marked as being at this revision
without running the migration.

Revision ID: 0001
Revises: None (base migration)
Create Date: 2025-01-01

IMPORTANT - Downgrade Behavior:
================================
WARNING: The downgrade() function in this migration is DESTRUCTIVE.
Running `command.downgrade(config, "base")` will DROP ALL TABLES.

This means:
- All user data will be permanently lost
- All research history, settings, and configurations will be deleted
- This is NOT recoverable without a database backup

The downgrade is provided for development/testing purposes only.
In production environments:
1. ALWAYS backup the database before any migration operation
2. NEVER run downgrade to base on production databases
3. Prefer restoring from backup over downgrading
"""

from alembic import op
from loguru import logger
from sqlalchemy import inspect

# revision identifiers, used by Alembic.
revision = "0001"
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    """
    Create all tables from models.

    Uses SQLAlchemy's metadata.create_all() to create tables that don't exist.
    This is a baseline migration - future migrations will use explicit
    op.create_table() / op.alter_table() calls.
    """
    # Import Base here to avoid circular imports at module level
    from local_deep_research.database.models import Base

    # Get the connection from alembic context
    connection = op.get_bind()

    # Check which tables already exist
    inspector = inspect(connection)
    existing_tables = set(inspector.get_table_names())

    # Create only tables that don't exist
    # This makes the migration idempotent
    tables_to_create = []
    for table in Base.metadata.sorted_tables:
        # Skip the users table - it's only in the auth database
        if table.name == "users":
            continue
        if table.name not in existing_tables:
            tables_to_create.append(table)

    if tables_to_create:
        Base.metadata.create_all(connection, tables=tables_to_create)


def downgrade():
    """
    ⚠️  DESTRUCTIVE OPERATION - Drop all tables.

    WARNING: This will permanently delete ALL data in the database.
    This includes: research history, settings, API keys, benchmarks,
    news subscriptions, and all other user data.

    This function is provided for development and testing purposes only.
    In production environments, prefer restoring from backup rather than
    running this downgrade.

    There is no undo for this operation without a database backup.
    """
    # Import Base here to avoid circular imports at module level
    from local_deep_research.database.models import Base

    connection = op.get_bind()

    # Drop tables in reverse dependency order
    for table in reversed(Base.metadata.sorted_tables):
        # Skip the users table - it's only in the auth database
        if table.name == "users":
            continue
        try:
            table.drop(connection, checkfirst=True)
        except Exception:
            logger.debug(f"Could not drop table {table.name} during downgrade")
