from datetime import datetime
from typing import Any

from pydantic import BaseModel, Field


class SpeakerTurn(BaseModel):
    speaker: str
    text: str
    timestamp: datetime | None = None


class IngestCallRequest(BaseModel):
    tenant_id: str
    call_id: str
    agent_id: str
    timestamp: datetime
    tags: list[str] = Field(default_factory=list)
    transcript: list[SpeakerTurn]


class IngestCallResponse(BaseModel):
    tenant_id: str
    call_id: str
    chunks_ingested: int


class QueryRequest(BaseModel):
    tenant_id: str
    user_id: str
    query: str
    tags: list[str] = Field(default_factory=list)
    top_k: int | None = None
    use_keyword: bool = True


class ContextChunk(BaseModel):
    chunk_id: str
    call_id: str
    agent_id: str
    speaker: str
    text: str
    score: float
    metadata: dict[str, Any]


class QueryResponse(BaseModel):
    answer: str
    context: list[ContextChunk]


class ScoreCallRequest(BaseModel):
    tenant_id: str
    call_id: str
    agent_id: str
    query_hint: str | None = None


class ScoreCallResponse(BaseModel):
    tenant_id: str
    call_id: str
    score_payload: dict[str, Any]


class AgentReportResponse(BaseModel):
    tenant_id: str
    agent_id: str
    total_calls_scored: int
    avg_quality_score: float
    compliance_rate: float
    recurring_issues: list[str]
