"""persist document generation preference on conversations

Revision ID: a3647ca77972
Revises: 93647ca77971
Create Date: 2026-08-05 17:00:00.000000
"""

import os
import sys
from collections.abc import Sequence

import sqlalchemy as sa

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from utils import batch_alter_table, migration_logger
from utils import table_exists as _table_exists

logger = migration_logger

revision: str = "a3647ca77972"
down_revision: str | Sequence[str] | None = "93647ca77971"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    if not _table_exists("conversations"):
        logger.warning("conversations table not found; skipping document generation preference migration")
        return

    with batch_alter_table("conversations") as batch_op:
        batch_op.add_column(
            sa.Column(
                "doc_gen_enabled",
                sa.Boolean(),
                nullable=False,
                server_default=sa.true(),
            )
        )


def downgrade() -> None:
    if not _table_exists("conversations"):
        logger.warning("conversations table not found; skipping document generation preference downgrade")
        return

    with batch_alter_table("conversations") as batch_op:
        batch_op.drop_column("doc_gen_enabled")
