from datetime import date

from sqlalchemy import Date, ForeignKey, Index, Integer, String
from sqlalchemy.orm import Mapped, mapped_column

from src.db.base_class import Base


class LLMUsageLog(Base):
    __tablename__ = "llm_usage_log"

    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    provider_id: Mapped[int | None] = mapped_column(
        ForeignKey("llm_provider.id", ondelete="SET NULL"), nullable=True
    )
    provider_name_snapshot: Mapped[str | None] = mapped_column(String(128), nullable=True)
    usage_date: Mapped[date] = mapped_column(Date, nullable=False)
    scene: Mapped[str] = mapped_column(String(64), nullable=False)
    prompt_tokens: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
    completion_tokens: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
    total_tokens: Mapped[int] = mapped_column(Integer, nullable=False, default=0)

    __table_args__ = (
        Index("idx_llm_usage_log_date", "usage_date"),
        Index("idx_llm_usage_log_provider_date", "provider_id", "usage_date"),
    )
