"""AI usage tracking model for analytics."""
import uuid
from datetime import datetime
from typing import Optional

from sqlalchemy import Column, DateTime, ForeignKey, Integer, String
from sqlmodel import Field, SQLModel

from utils.datetime_utils import get_current_utc_datetime


class AIUsageModel(SQLModel, table=True):
    __tablename__ = "ai_usage_logs"

    id: uuid.UUID = Field(primary_key=True, default_factory=uuid.uuid4)
    user_id: Optional[uuid.UUID] = Field(
        sa_column=Column(ForeignKey("users.id"), nullable=True), default=None
    )
    client_id: Optional[uuid.UUID] = Field(
        sa_column=Column(ForeignKey("clients.id"), nullable=True), default=None
    )
    presentation_id: Optional[uuid.UUID] = Field(
        sa_column=Column(ForeignKey("presentations.id"), nullable=True), default=None
    )
    provider: str = Field(sa_column=Column(String, nullable=False))
    model: str = Field(sa_column=Column(String, nullable=False))
    call_type: str = Field(sa_column=Column(String, nullable=False))
    input_tokens: Optional[int] = Field(sa_column=Column(Integer, nullable=True), default=None)
    output_tokens: Optional[int] = Field(sa_column=Column(Integer, nullable=True), default=None)
    total_tokens: Optional[int] = Field(sa_column=Column(Integer, nullable=True), default=None)
    duration_ms: Optional[int] = Field(sa_column=Column(Integer, nullable=True), default=None)
    error_details: Optional[str] = Field(sa_column=Column(String, nullable=True), default=None)
    created_at: datetime = Field(
        sa_column=Column(
            DateTime(timezone=True), nullable=False, default=get_current_utc_datetime
        ),
    )
