"""aider-watch event pydantic schemas。由 Mac client POST 送來。"""
from __future__ import annotations
from datetime import datetime
from typing import Any, Literal
from pydantic import BaseModel, ConfigDict, Field, field_validator

EventType = Literal[
    "session_start", "file_edit", "error", "commit",
    "silent_timeout", "session_end", "raw",
]


class AiderEventIn(BaseModel):
    """單筆 aider event 輸入 schema。"""
    model_config = ConfigDict(frozen=True, extra="forbid")

    ts: datetime
    session_id: str = Field(min_length=1, max_length=64)
    host: str = Field(default="ogt-mac", max_length=64)
    type: EventType
    payload: dict[str, Any] = Field(default_factory=dict)

    @field_validator("ts")
    @classmethod
    def _ensure_tz(cls, v: datetime) -> datetime:
        if v.tzinfo is None:
            raise ValueError("ts must be timezone-aware")
        return v


class AiderBatchIn(BaseModel):
    """批次 event 輸入（最多 50 筆/次）。"""
    model_config = ConfigDict(frozen=True, extra="forbid")

    events: list[AiderEventIn] = Field(min_length=1, max_length=50)
