"""Add confidence column to domain_term_dictionary.

Adds a ``confidence FLOAT NOT NULL`` column with a ``CHECK (0..1)`` constraint.
Existing rows are backfilled with 0.70 (Yellow band) via a temporary
``server_default`` that is dropped after the backfill, so future inserts must
supply a value explicitly (enforced at the ORM/Pydantic layer).

Revision ID: 5e6f7a8b9cad
Revises: 4d5e6f7a8b9c
Create Date: 2026-04-22 10:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


revision: str = "5e6f7a8b9cad"
down_revision: Union[str, None] = "4d5e6f7a8b9c"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.add_column(
        "domain_term_dictionary",
        sa.Column(
            "confidence",
            sa.Float(),
            nullable=False,
            server_default=sa.text("0.70"),
        ),
    )
    op.create_check_constraint(
        "ck_domain_term_confidence_range",
        "domain_term_dictionary",
        "confidence >= 0 AND confidence <= 1",
    )
    op.alter_column("domain_term_dictionary", "confidence", server_default=None)


def downgrade() -> None:
    op.drop_constraint(
        "ck_domain_term_confidence_range",
        "domain_term_dictionary",
        type_="check",
    )
    op.drop_column("domain_term_dictionary", "confidence")
