"""SQLAlchemy models for metrics."""

from sqlalchemy import (
    Column,
    DateTime,
    Integer,
    String,
    Text,
    UniqueConstraint,
    func,
)
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class TokenUsage(Base):
    """Model for tracking individual token usage events."""

    __tablename__ = "token_usage"

    id = Column(Integer, primary_key=True)
    research_id = Column(
        Integer
    )  # Removed foreign key constraint to fix token tracking
    model_name = Column(String)
    provider = Column(
        String
    )  # Added provider column for accurate cost tracking
    prompt_tokens = Column(Integer)
    completion_tokens = Column(Integer)
    total_tokens = Column(Integer)

    # Phase 1 Enhancement: Research context
    research_query = Column(Text)
    research_mode = Column(String)  # 'quick' or 'detailed'
    research_phase = Column(String)  # 'init', 'iteration_1', etc.
    search_iteration = Column(Integer)

    # Phase 1 Enhancement: Performance metrics
    response_time_ms = Column(Integer)
    success_status = Column(
        String, default="success"
    )  # 'success', 'error', 'timeout'
    error_type = Column(String)

    # Phase 1 Enhancement: Search engine context
    search_engines_planned = Column(Text)  # JSON array as text
    search_engine_selected = Column(String)

    # Call stack tracking
    calling_file = Column(String)  # File that made the LLM call
    calling_function = Column(String)  # Function that made the LLM call
    call_stack = Column(Text)  # Full call stack as JSON

    timestamp = Column(DateTime, server_default=func.now())


class ModelUsage(Base):
    """Model for aggregated token usage by model and research."""

    __tablename__ = "model_usage"
    __table_args__ = (UniqueConstraint("research_id", "model_name"),)

    id = Column(Integer, primary_key=True)
    research_id = Column(
        Integer
    )  # Removed foreign key constraint to fix token tracking
    model_name = Column(String)
    provider = Column(String)
    prompt_tokens = Column(Integer, default=0)
    completion_tokens = Column(Integer, default=0)
    total_tokens = Column(Integer, default=0)
    calls = Column(Integer, default=0)
    timestamp = Column(DateTime, server_default=func.now())


class ResearchRating(Base):
    """Model for storing user ratings of research sessions."""

    __tablename__ = "research_ratings"

    id = Column(Integer, primary_key=True)
    research_id = Column(Integer, unique=True)  # References research session ID
    rating = Column(Integer)  # 1-5 star rating
    rated_at = Column(DateTime, server_default=func.now())
    updated_at = Column(
        DateTime, server_default=func.now(), onupdate=func.now()
    )


class SearchCall(Base):
    """Model for individual search engine calls."""

    __tablename__ = "search_calls"

    id = Column(Integer, primary_key=True)
    research_id = Column(Integer)
    research_query = Column(Text)
    research_mode = Column(String)
    research_phase = Column(String)
    search_iteration = Column(Integer)

    # Search details
    search_engine = Column(String)
    query = Column(Text)
    results_count = Column(Integer)
    response_time_ms = Column(Integer)

    # Status tracking
    success_status = Column(String, default="success")
    error_type = Column(String)
    error_message = Column(Text)

    timestamp = Column(DateTime, server_default=func.now())
