from sqlalchemy import BIGINT, FLOAT, TIMESTAMP, Index, Integer, JSON, String, text
from sqlalchemy.orm import Mapped, mapped_column

from app.database import Base


class EvalRecord(Base):
    __tablename__ = 'eval_records'
    __table_args__ = (
        Index('idx_eval_model', 'model_id'),
        Index('idx_eval_package', 'package_id'),
        {'mysql_comment': '模型评估记录表'},
    )

    id: Mapped[int] = mapped_column(BIGINT, primary_key=True, autoincrement=True)
    model_id: Mapped[int] = mapped_column(BIGINT, nullable=False, comment='已保存模型ID')
    model_name: Mapped[str] = mapped_column(String(255), nullable=False, comment='模型名称')
    package_id: Mapped[int] = mapped_column(BIGINT, nullable=False, comment='数据包ID')
    package_name: Mapped[str] = mapped_column(String(255), nullable=False, comment='数据包名称')
    total_count: Mapped[int] = mapped_column(Integer, nullable=False, server_default=text('0'), comment='评估数据点总数')
    mae: Mapped[float | None] = mapped_column(FLOAT, nullable=True, comment='平均绝对误差')
    mse: Mapped[float | None] = mapped_column(FLOAT, nullable=True, comment='均方误差')
    rmse: Mapped[float | None] = mapped_column(FLOAT, nullable=True, comment='均方根误差')
    mape: Mapped[float | None] = mapped_column(FLOAT, nullable=True, comment='平均绝对百分比误差(%)')
    r2: Mapped[float | None] = mapped_column(FLOAT, nullable=True, comment='决定系数')
    # Store up to ~2000 sampled points for chart rendering
    chart_data: Mapped[list | None] = mapped_column(JSON, nullable=True, comment='图表数据(采样)')
    created_at: Mapped[str] = mapped_column(TIMESTAMP, nullable=False, server_default=text('CURRENT_TIMESTAMP'))
