from __future__ import annotations

from datetime import datetime

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

from supply_infra.db.base import Base


class DemandFeedback(Base):
    """需求汇总页面的人工反馈记录。"""

    __tablename__ = "demand_feedback"
    __table_args__ = (
        UniqueConstraint(
            "client_request_id",
            name="uk_demand_feedback_request",
        ),
        Index(
            "idx_demand_feedback_demand",
            "demand_grade_id",
            "created_at",
        ),
        Index(
            "idx_demand_feedback_video",
            "demand_grade_id",
            "video_id",
            "created_at",
        ),
        Index(
            "idx_demand_feedback_expansion",
            "demand_video_expansion_id",
            "created_at",
        ),
        Index(
            "idx_demand_feedback_user",
            "feedback_user_id",
            "created_at",
        ),
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    client_request_id: Mapped[str] = mapped_column(String(64), nullable=False)
    target_type: Mapped[str] = mapped_column(
        String(32),
        nullable=False,
        comment="反馈对象：demand / video / hit_content",
    )
    biz_dt: Mapped[str] = mapped_column(
        String(32),
        nullable=False,
        comment="目标所属业务日",
    )
    demand_grade_id: Mapped[int] = mapped_column(
        BigInteger,
        nullable=False,
        comment="目标 demand_grade.id",
    )
    video_id: Mapped[str | None] = mapped_column(
        String(64),
        nullable=True,
        comment="视频反馈或命中内容反馈的视频 id",
    )
    demand_video_expansion_id: Mapped[int | None] = mapped_column(
        BigInteger,
        nullable=True,
        comment="命中内容 demand_video_expansion.id",
    )
    feedback_action: Mapped[str] = mapped_column(
        String(32),
        nullable=False,
        comment="support / oppose / correct / supplement",
    )
    reason_code: Mapped[str | None] = mapped_column(String(64), nullable=True)
    content: Mapped[str | None] = mapped_column(Text, nullable=True)
    target_snapshot_json: Mapped[str] = mapped_column(
        Text,
        nullable=False,
        comment="提交时由服务端生成的目标快照",
    )
    feedback_user_id: Mapped[int] = mapped_column(
        Integer,
        nullable=False,
        comment="反馈人 auth_user.id",
    )
    feedback_user_name_snapshot: Mapped[str] = mapped_column(
        String(128),
        nullable=False,
        comment="反馈人名称快照",
    )
    feedback_username_snapshot: Mapped[str] = mapped_column(
        String(64),
        nullable=False,
        comment="反馈人登录账号快照",
    )
    created_at: Mapped[datetime] = mapped_column(
        DateTime,
        nullable=False,
        server_default=func.now(),
    )
    updated_at: Mapped[datetime] = mapped_column(
        DateTime,
        nullable=False,
        server_default=func.now(),
        onupdate=func.now(),
    )
