"""add guide_candidate_dismissals (tenant table)

Records guide candidates the user Discarded, keyed by the representative
thread_id of the dismissed cluster. Detection suppresses future clusters whose
representative is cosine-close to a dismissed one.

Tenant-schema migration: runs against the active tenant search_path and no-ops
on public (same pattern as ar002), since this is a per-tenant object.

Revision ID: gcd001
Revises: ar003
Create Date: 2026-07-28
"""
from alembic import op
import sqlalchemy as sa

revision = 'gcd001'
down_revision = 'ar003'
branch_labels = None
depends_on = None


def _in_public_schema(conn):
    return conn.execute(sa.text("SELECT current_schema()")).scalar() == 'public'


def upgrade():
    conn = op.get_bind()
    if _in_public_schema(conn):
        return
    insp = sa.inspect(conn)
    if 'guide_candidate_dismissals' not in set(insp.get_table_names()):
        op.create_table(
            'guide_candidate_dismissals',
            sa.Column('thread_id', sa.Integer, primary_key=True),
            sa.Column('dismissed_by', sa.Integer, nullable=True),
            sa.Column('dismissed_at', sa.DateTime, nullable=False,
                      server_default=sa.func.now()),
            sa.ForeignKeyConstraint(['thread_id'], ['addons_threads.id']),
        )


def downgrade():
    conn = op.get_bind()
    if _in_public_schema(conn):
        return
    op.drop_table('guide_candidate_dismissals')
