"""
Pydantic schemas for financial APIs
"""
from typing import Dict, List, Optional
from pydantic import BaseModel


class PeriodDataPoint(BaseModel):
    period: str
    value: Optional[float]


class StepRecord(BaseModel):
    name: str
    start_ts: str  # ISO8601
    end_ts: Optional[str] = None
    duration_ms: Optional[int] = None
    status: str  # running|done|error
    error: Optional[str] = None


class FinancialMeta(BaseModel):
    started_at: str  # ISO8601
    finished_at: Optional[str] = None
    elapsed_ms: Optional[int] = None
    api_calls_total: int = 0
    api_calls_by_group: Dict[str, int] = {}
    current_action: Optional[str] = None
    steps: List[StepRecord] = []


class BatchFinancialDataResponse(BaseModel):
    ts_code: str
    name: Optional[str] = None
    series: Dict[str, List[PeriodDataPoint]]
    meta: Optional[FinancialMeta] = None


class FinancialConfigResponse(BaseModel):
    api_groups: Dict[str, List[dict]]


class TokenUsage(BaseModel):
    prompt_tokens: int = 0
    completion_tokens: int = 0
    total_tokens: int = 0


class CompanyProfileResponse(BaseModel):
    ts_code: str
    company_name: Optional[str] = None
    content: str
    model: str
    tokens: TokenUsage
    elapsed_ms: int
    success: bool = True
    error: Optional[str] = None


class AnalysisResponse(BaseModel):
    ts_code: str
    company_name: Optional[str] = None
    analysis_type: str
    content: str
    model: str
    tokens: TokenUsage
    elapsed_ms: int
    success: bool = True
    error: Optional[str] = None


class AnalysisConfigResponse(BaseModel):
    analysis_modules: Dict[str, Dict]


class TodaySnapshotResponse(BaseModel):
    ts_code: str
    trade_date: str
    name: Optional[str] = None
    close: Optional[float] = None
    pe: Optional[float] = None
    pb: Optional[float] = None
    dv_ratio: Optional[float] = None
    total_mv: Optional[float] = None


class RealTimeQuoteResponse(BaseModel):
    symbol: str
    market: str
    ts: str
    price: float
    open_price: Optional[float] = None
    high_price: Optional[float] = None
    low_price: Optional[float] = None
    prev_close: Optional[float] = None
    change: Optional[float] = None
    change_percent: Optional[float] = None
    volume: Optional[int] = None
    source: Optional[str] = None
