"""AI Call Log model - records every Kimi API call for cost tracking."""
from datetime import datetime
from sqlalchemy import Column, Integer, String, Text, DateTime
from ..database import Base


class AICallLog(Base):
    """Log of every AI API call with token usage and cost."""
    __tablename__ = "ai_call_logs"

    id = Column(Integer, primary_key=True, index=True)
    endpoint = Column(String(100), nullable=False, index=True)
    model = Column(String(50), nullable=False, index=True)
    prompt_preview = Column(Text, nullable=True)
    response_preview = Column(Text, nullable=True)
    error = Column(Text, nullable=True)
    duration_ms = Column(Integer, default=0)
    prompt_tokens = Column(Integer, default=0)
    completion_tokens = Column(Integer, default=0)
    total_tokens = Column(Integer, default=0)
    status = Column(String(20), default="success", index=True)  # success/failed
    created_at = Column(DateTime, default=datetime.utcnow, index=True)

    def __repr__(self):
        return f"<AICallLog id={self.id} model={self.model} status={self.status} tokens={self.total_tokens}>"
