"""
年度收视目标模型 — SQLModel

业务规则（.clinerules 5.2 年度目标只增不改）:
  - yearly_targets.year 有 UNIQUE 约束，数据库层已保证
  - 代码端 INSERT 时只许 ON CONFLICT DO NOTHING，禁止 ON CONFLICT UPDATE
  - 历史年份目标行绝不可被覆盖或删除
  - 前端判颜色时按播出年份取对应年的目标，不是当前年份
"""

from datetime import date, datetime

from sqlalchemy import Column
from sqlalchemy import DateTime as SADateTime
from sqlalchemy import Integer as SAInteger
from sqlalchemy import Numeric as SANumeric
from sqlalchemy.sql import func as sa_func
from sqlmodel import Field, SQLModel


class YearlyTarget(SQLModel, table=True):
    __tablename__ = "yearly_targets"

    id: int | None = Field(default=None, primary_key=True)
    year: int = Field(unique=True, index=True)
    base_target: float  # 基础目标
    stretch_target: float  # 摸高目标
    issued_date: date | None = Field(default=None)
    notes: str | None = Field(default=None)

    created_at: datetime | None = Field(
        default=None,
        sa_column=Column(SADateTime(timezone=True), nullable=False, server_default=sa_func.now()),
    )