"""Pydantic models for request/response validation."""

from __future__ import annotations

from pydantic import BaseModel, Field


# -- Admin ------------------------------------------------------------------

class CreateKeyRequest(BaseModel):
    org_name: str = Field(..., min_length=1, max_length=256)


class CreateKeyResponse(BaseModel):
    id: str
    api_key: str  # raw key, shown only once
    org_name: str


# -- Receipts ---------------------------------------------------------------

class ProofPathEntry(BaseModel):
    side: str = Field(..., pattern="^(LEFT|RIGHT)$")
    sibling_hash: str = Field(..., min_length=64, max_length=64)


class ReceiptSubmission(BaseModel):
    index: int = Field(..., ge=0)
    timestamp: str
    agent_id: str | None = None
    tool_name: str
    query_hash: str = Field(..., min_length=64, max_length=64)
    response_hash: str = Field(..., min_length=64, max_length=64)
    record_hash: str = Field(..., min_length=64, max_length=64)
    root_hash: str = Field(..., min_length=64, max_length=64)
    proof: list[ProofPathEntry]
    proof_root: str = Field(..., min_length=64, max_length=64)


class ReceiptResponse(BaseModel):
    verified: bool
    index: int
    record_hash: str
    root_hash: str


class ReceiptDetail(BaseModel):
    index: int
    timestamp: str
    agent_id: str | None
    tool_name: str
    query_hash: str
    response_hash: str
    record_hash: str
    root_hash: str
    proof: list[ProofPathEntry]
    proof_root: str
    received_at: str


class ReceiptListResponse(BaseModel):
    receipts: list[ReceiptDetail]
    total: int
    offset: int
    limit: int


# -- Verify -----------------------------------------------------------------

class VerifyRequest(BaseModel):
    record_hash: str = Field(..., min_length=64, max_length=64)
    proof: list[ProofPathEntry]
    proof_root: str = Field(..., min_length=64, max_length=64)


class VerifyResponse(BaseModel):
    valid: bool
    anchored: bool
    anchor_created_at: str | None = None


# -- Anchors ----------------------------------------------------------------

class AnchorRequest(BaseModel):
    root_hash: str = Field(..., min_length=64, max_length=64)
    receipt_count: int = Field(..., ge=0)


class AnchorResponse(BaseModel):
    root_hash: str
    receipt_count: int
    created_at: str
