"""Drop dead Document.notes legacy column from the documents table

The ``notes`` column on the ``documents`` table is a dead legacy column.
It was introduced by the Research Library feature (Issue #207, commit
3e8cef7cf) and ships to all released users, but it has zero callers in
``src/`` -- nothing reads from or writes to it.  Its name collides
vocabulary-wise with the separate notes-v2 feature (Document rows with
``source_type='note'`` plus the ``note_*`` tables), but it is an
unrelated concept and is safe to remove on its own.

Because the column already exists in every released user's database, it
cannot be removed as a plain model edit -- it needs a real migration
that runs on existing ``documents`` tables.

Revision ID: 0015
Revises: 0014
Create Date: 2026-06-15

Column Dropped:
===============
- notes (TEXT): Unused per-document user notes column.  Nullable.

Migration Notes:
===============
- Uses SQLite batch mode (table recreation) for ALTER TABLE operations,
  consistent with the repo's other migrations.
- Idempotent: skips the drop if the column is already gone (fresh
  databases created after the model change never had it).

Downgrade Behavior:
==================
The downgrade() function re-adds the notes column as a nullable Text
column.  No data is restored (the column carried no data in use).
"""

from alembic import op
import sqlalchemy as sa
from sqlalchemy import inspect

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


def column_exists(table_name: str, column_name: str) -> bool:
    """Check if a column exists in a table."""
    bind = op.get_bind()
    inspector = inspect(bind)

    if not inspector.has_table(table_name):
        return False

    columns = {col["name"] for col in inspector.get_columns(table_name)}
    return column_name in columns


def upgrade():
    """Drop the dead notes column from documents if it exists."""
    bind = op.get_bind()
    inspector = inspect(bind)

    if not inspector.has_table("documents"):
        # Table doesn't exist yet, will be created by initial migration
        return

    if not column_exists("documents", "notes"):
        # Column already gone (fresh database created after model change)
        return

    # Use batch mode for SQLite compatibility
    with op.batch_alter_table("documents") as batch_op:
        batch_op.drop_column("notes")


def downgrade():
    """Re-add the notes column to documents as nullable Text."""
    bind = op.get_bind()
    inspector = inspect(bind)

    if not inspector.has_table("documents"):
        return

    if column_exists("documents", "notes"):
        return

    with op.batch_alter_table("documents") as batch_op:
        batch_op.add_column(sa.Column("notes", sa.Text(), nullable=True))
