from __future__ import annotations

import uuid
from dataclasses import dataclass, field
from datetime import datetime, timezone


@dataclass
class QueryRecord:
    """Captured data for a single database query."""

    sql: str = ""
    params: str | None = None  # str repr of original params tuple/dict
    duration_ms: float = 0.0
    stack_trace: str | None = None


@dataclass
class ProfileStat:
    """Call-stack stats for a single function from cProfile."""

    file: str = ""  # path relative to cwd
    line: int = 0
    func: str = ""  # co_qualname
    prim_calls: int = 0  # non-recursive call count
    calls: int = 0  # total call count
    tt_ms: float = 0.0  # own time (exclusive of callees), ms
    ct_ms: float = 0.0  # cumulative time (inclusive of callees), ms
    # top-3 callers by ct_ms: [(file, func, line, ct_ms), ...]
    callers: list = field(default_factory=list)


@dataclass
class RequestRecord:
    """Captured data for a single HTTP request."""

    request_id: str = field(default_factory=lambda: str(uuid.uuid4()))
    timestamp: datetime = field(default_factory=lambda: datetime.now(timezone.utc))
    method: str = ""
    path: str = ""
    query_string: str = ""
    status_code: int = 0
    duration_ms: float = 0.0
    request_headers: dict[str, str] = field(default_factory=dict)
    response_headers: dict[str, str] = field(default_factory=dict)
    client_host: str = ""
    queries: list[QueryRecord] = field(default_factory=list)
    response_body: str | None = None
    profile_stats: list[ProfileStat] | None = None  # None = profiling not enabled
