"""TagAllowlist — tags the operator opted-in to auto-apply via maintenance."""

from datetime import datetime

from sqlalchemy import CheckConstraint, DateTime, Float, ForeignKey, func
from sqlalchemy.orm import Mapped, mapped_column

from .base import Base


class TagAllowlist(Base):
    __tablename__ = "tag_allowlist"
    # Bare name — Base.metadata's naming convention prepends ck_<table>_,
    # producing the final ck_tag_allowlist_confidence_range (matches migration 0003).
    __table_args__ = (
        CheckConstraint(
            "min_confidence > 0 AND min_confidence <= 1",
            name="confidence_range",
        ),
    )

    tag_id: Mapped[int] = mapped_column(
        ForeignKey("tag.id", ondelete="CASCADE"), primary_key=True
    )
    # Default auto-apply threshold for a newly-accepted tag. 0.90 (lowered from
    # 0.95 on operator evidence 2026-06-07: 0.95 was too strict and skipped
    # confident-enough applications). Per-tag value is still tunable in the
    # allowlist table; existing rows keep whatever they were stored with.
    min_confidence: Mapped[float] = mapped_column(Float, nullable=False, default=0.90)
    added_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), nullable=False, server_default=func.now()
    )
