from __future__ import annotations

from datetime import datetime
from decimal import Decimal

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

from supply_infra.db.base import Base


class CategoryTreeWeight(Base):
    """类目树节点热度（按日，六维加权平均分 avg + count）。"""

    __tablename__ = "category_tree_weight"
    __table_args__ = (
        UniqueConstraint("category_id", "biz_dt", name="uk_category_tree_weight"),
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    category_id: Mapped[int] = mapped_column(
        BigInteger, nullable=False, comment="global_tree_category.id"
    )
    biz_dt: Mapped[str] = mapped_column(String(32), nullable=False, comment="日期YYYYMMDD")
    hung_word_count: Mapped[int] = mapped_column(
        Integer, nullable=False, default=0, comment="挂载需求词数量"
    )

    ext_pop_avg: Mapped[Decimal | None] = mapped_column(
        Numeric(16, 8), nullable=True, comment="外部热度-加权平均分"
    )
    ext_pop_count: Mapped[int] = mapped_column(
        Integer, nullable=False, default=0, comment="外部热度-样本数"
    )

    plat_sust_pop_avg: Mapped[Decimal | None] = mapped_column(
        Numeric(16, 8), nullable=True, comment="平台持续热度-加权平均分"
    )
    plat_sust_pop_count: Mapped[int] = mapped_column(
        Integer, nullable=False, default=0, comment="平台持续热度-样本数"
    )

    plat_ly_pop_avg: Mapped[Decimal | None] = mapped_column(
        Numeric(16, 8), nullable=True, comment="平台去年同期-加权平均分"
    )
    plat_ly_pop_count: Mapped[int] = mapped_column(
        Integer, nullable=False, default=0, comment="平台去年同期-样本数"
    )

    recent_pop_avg: Mapped[Decimal | None] = mapped_column(
        Numeric(16, 8), nullable=True, comment="近期热度-加权平均分"
    )
    recent_pop_count: Mapped[int] = mapped_column(
        Integer, nullable=False, default=0, comment="近期热度-样本数"
    )

    ext_pop_score: Mapped[Decimal | None] = mapped_column(
        Numeric(16, 8), nullable=True, comment="外部热度-排名归一化分"
    )
    plat_sust_pop_score: Mapped[Decimal | None] = mapped_column(
        Numeric(16, 8), nullable=True, comment="平台持续热度-排名归一化分"
    )
    plat_ly_pop_score: Mapped[Decimal | None] = mapped_column(
        Numeric(16, 8), nullable=True, comment="平台去年同期-排名归一化分"
    )
    recent_pop_score: Mapped[Decimal | None] = mapped_column(
        Numeric(16, 8), nullable=True, comment="近期热度-排名归一化分"
    )
    total_score: Mapped[Decimal | None] = mapped_column(
        Numeric(16, 8), nullable=True, comment="四维排名分之和"
    )

    real_rov_7d_avg: Mapped[Decimal | None] = mapped_column(
        Numeric(16, 8), nullable=True, comment="近7日ROV相对全局diff-加权平均分"
    )
    real_rov_7d_count: Mapped[int] = mapped_column(
        Integer, nullable=False, default=0, comment="近7日ROV相对全局diff-样本数"
    )
    real_vov_7d_avg: Mapped[Decimal | None] = mapped_column(
        Numeric(16, 8), nullable=True, comment="近7日VOV相对全局diff-加权平均分"
    )
    real_vov_7d_count: Mapped[int] = mapped_column(
        Integer, nullable=False, default=0, comment="近7日VOV相对全局diff-样本数"
    )

    created_time: Mapped[datetime] = mapped_column(
        nullable=False,
        server_default=func.now(),
        comment="创建时间",
    )
    updated_time: Mapped[datetime] = mapped_column(
        nullable=False,
        server_default=func.now(),
        onupdate=func.now(),
        comment="更新时间",
    )
