from __future__ import annotations

from datetime import datetime

from sqlalchemy import BigInteger, Index, Integer, String, Text, UniqueConstraint, func
from sqlalchemy.orm import Mapped, mapped_column

from supply_infra.db.base import Base


class DemandVideoExpansion(Base):
    """S/A 需求关联视频点位拓展结果。"""

    __tablename__ = "demand_video_expansion"
    __table_args__ = (
        UniqueConstraint(
            "biz_dt",
            "source_demand_grade_id",
            "expanded_text",
            "video_id",
            name="uk_demand_video_expansion",
        ),
        Index("idx_dve_biz_dt", "biz_dt"),
        Index("idx_dve_source_grade", "source_demand_grade_id"),
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    biz_dt: Mapped[str] = mapped_column(String(32), nullable=False, comment="业务日 YYYYMMDD")
    run_id: Mapped[str] = mapped_column(String(64), nullable=False, comment="任务批次 run_id")
    source_demand_grade_id: Mapped[int] = mapped_column(
        BigInteger, nullable=False, comment="来源 demand_grade.id"
    )
    source_demand_name: Mapped[str] = mapped_column(
        String(256), nullable=False, comment="来源需求名"
    )
    source_grade: Mapped[str] = mapped_column(String(4), nullable=False, comment="来源等级 S/A")
    expanded_text: Mapped[str] = mapped_column(
        String(512), nullable=False, comment="拓展需求文本（来自 point_data）"
    )
    point_type: Mapped[str] = mapped_column(
        String(32), nullable=False, comment="点类型：inspiration / purpose / key"
    )
    point_desc: Mapped[str | None] = mapped_column(Text, nullable=True, comment="点位描述快照")
    video_id: Mapped[str] = mapped_column(String(64), nullable=False, comment="来源视频 id")
    reason: Mapped[str] = mapped_column(Text, nullable=False, comment="相近判断依据")
    is_delete: Mapped[int] = mapped_column(
        Integer, default=0, nullable=False, comment="是否删除 0-正常 1-删除"
    )
    create_time: Mapped[datetime] = mapped_column(
        nullable=False,
        server_default=func.now(),
        comment="创建时间",
    )
    update_time: Mapped[datetime] = mapped_column(
        nullable=False,
        server_default=func.now(),
        onupdate=func.now(),
        comment="更新时间",
    )


class DemandVideoExpansionRun(Base):
    """记录每个需求是否已完成拓展判断（含零结果），用于幂等跳过。"""

    __tablename__ = "demand_video_expansion_run"
    __table_args__ = (
        UniqueConstraint(
            "biz_dt",
            "source_demand_grade_id",
            name="uk_demand_video_expansion_run",
        ),
        Index("idx_dver_biz_dt", "biz_dt"),
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    biz_dt: Mapped[str] = mapped_column(String(32), nullable=False, comment="业务日 YYYYMMDD")
    run_id: Mapped[str] = mapped_column(String(64), nullable=False, comment="任务批次 run_id")
    source_demand_grade_id: Mapped[int] = mapped_column(
        BigInteger, nullable=False, comment="来源 demand_grade.id"
    )
    saved_count: Mapped[int] = mapped_column(
        Integer, nullable=False, default=0, comment="落库拓展条数"
    )
    status: Mapped[str] = mapped_column(
        String(16), nullable=False, default="finished", comment="finished / failed"
    )
    error_message: Mapped[str | None] = mapped_column(
        Text, nullable=True, comment="失败原因"
    )
    create_time: Mapped[datetime] = mapped_column(
        nullable=False,
        server_default=func.now(),
        comment="创建时间",
    )
    update_time: Mapped[datetime] = mapped_column(
        nullable=False,
        server_default=func.now(),
        onupdate=func.now(),
        comment="更新时间",
    )
