from __future__ import annotations

from datetime import datetime

from sqlalchemy import CheckConstraint, DateTime, Index, Text, func
from sqlalchemy.orm import Mapped, mapped_column

from app.db.base import Base


class Content(Base):
    """
    文案主体表。

    多语言约束：
    - 当前仅支持 EN / TC（繁体中文）
    - 至少需要提供 `text_en` 或 `text_tc` 之一
    """

    __tablename__ = "contents"

    __table_args__ = (
        CheckConstraint(
            "(text_en IS NOT NULL) OR (text_tc IS NOT NULL)",
            name="chk_contents_text_present",
        ),
        Index("idx_contents_author_id", "author_id"),
        Index("idx_contents_template_id", "template_id"),
    )

    content_id: Mapped[int] = mapped_column(
        primary_key=True,
        autoincrement=True,
        comment="文案唯一 ID（自增；文案微调时保持不变）",
    )

    text_en: Mapped[str | None] = mapped_column(
        Text,
        nullable=True,
        comment="英文文案（可空；若为空则必须提供 text_tc）",
    )
    text_tc: Mapped[str | None] = mapped_column(
        Text,
        nullable=True,
        comment="繁体中文文案（可空；若为空则必须提供 text_en）",
    )

    author_id: Mapped[str | None] = mapped_column(
        nullable=True,
        comment="作者/来源 ID（可空；用于多样性与频控）",
    )
    template_id: Mapped[str | None] = mapped_column(
        nullable=True,
        comment="模板 ID（可空；用于多样性与频控）",
    )

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

