from __future__ import annotations

from datetime import datetime

from sqlalchemy import DateTime, ForeignKey, Index, UniqueConstraint, func
from sqlalchemy.orm import Mapped, mapped_column

from app.db.base import Base


class ContentRiskFlag(Base):
    """
    内容风险标记（risk_flags）关联表。

    命名约束（语义来源：句子文案打分规则）：
    - 仅允许 `unsafe_for_*` / `block_*` / `soft_*` 前缀
    - 旧 flag（如 `block_stage_unknown`）需在写入/读取层做映射
    """

    __tablename__ = "content_risk_flags"

    __table_args__ = (
        UniqueConstraint("content_id", "flag", name="uniq_content_flag"),
        Index("idx_flag", "flag"),
        Index("idx_content_id", "content_id"),
    )

    id: Mapped[int] = mapped_column(
        primary_key=True,
        autoincrement=True,
        comment="主键",
    )

    content_id: Mapped[int] = mapped_column(
        ForeignKey("contents.content_id", ondelete="CASCADE"),
        nullable=False,
        comment="FK -> contents.content_id",
    )

    flag: Mapped[str] = mapped_column(
        nullable=False,
        comment="风险标记（unsafe_for_* / block_* / soft_*）",
    )

    created_at: Mapped[datetime] = mapped_column(
        DateTime,
        nullable=False,
        server_default=func.now(),
        comment="创建时间",
    )

