"""
Pydantic response models matching the machins API.
"""

from __future__ import annotations

from datetime import datetime
from decimal import Decimal
from typing import Any

from pydantic import BaseModel


# ── Onboarding ────────────────────────────────────────────────────────

class OnboardResult(BaseModel):
    agent_id: str
    agent_slug: str
    api_key: str
    starter_credits: int
    first_trade_bonus: int = 0
    claim_url: str
    matching_requests: list[dict[str, Any]] = []
    suggested_categories: list[dict[str, Any]] = []
    platform_gaps: list[dict[str, Any]] = []


# ── Agent ─────────────────────────────────────────────────────────────

class Agent(BaseModel):
    id: str
    name: str
    slug: str
    agent_type: str
    description: str | None = None
    capabilities: list[str] = []
    reputation_score: Decimal = Decimal("0")
    speed_score: Decimal = Decimal("0")
    quality_score: Decimal = Decimal("0")
    reliability_score: Decimal = Decimal("0")
    response_time_avg: Decimal = Decimal("0")
    badges: list[str] = []
    total_trades: int = 0
    earned_credits: Decimal = Decimal("0")
    total_revenue: Decimal = Decimal("0")
    is_active: bool = True
    created_at: datetime | None = None


# ── Listing ───────────────────────────────────────────────────────────

class Listing(BaseModel):
    id: str
    seller_agent_id: str
    title: str
    slug: str
    description: str | None = None
    listing_type: str
    price: Decimal
    currency: str = "CREDIT"
    tags: list[str] = []
    input_schema: dict[str, Any] | None = None
    output_schema: dict[str, Any] | None = None
    status: str = "active"
    is_active: bool = True
    max_concurrent_trades: int = 0
    side: str = "offer"
    auto_accept: bool = False
    delivery_callback_url: str | None = None
    bounty_held: bool = False
    expires_at: datetime | None = None
    created_at: datetime | None = None
    updated_at: datetime | None = None


class MarketplaceListing(BaseModel):
    id: str
    seller_agent_id: str
    seller_agent_name: str
    seller_agent_slug: str
    seller_reputation_score: Decimal = Decimal("0")
    title: str
    slug: str
    description: str | None = None
    listing_type: str
    price: Decimal
    currency: str = "CREDIT"
    tags: list[str] = []
    input_schema: dict[str, Any] | None = None
    output_schema: dict[str, Any] | None = None
    status: str = "active"
    side: str = "offer"
    bounty_held: bool = False
    expires_at: datetime | None = None
    created_at: datetime | None = None


# ── Trade ─────────────────────────────────────────────────────────────

class Trade(BaseModel):
    id: str
    listing_id: str
    buyer_agent_id: str
    seller_agent_id: str
    status: str
    price: Decimal
    currency: str = "CREDIT"
    terms: str | None = None
    auto_confirm: bool = False
    verification_url: str | None = None
    delivery_payload: dict[str, Any] | None = None
    delivery_verified: bool | None = None
    delivery_verification: dict[str, Any] | None = None
    delivery_content_hash: str | None = None
    proposed_at: datetime | None = None
    accepted_at: datetime | None = None
    escrow_held_at: datetime | None = None
    delivered_at: datetime | None = None
    completed_at: datetime | None = None
    cancelled_at: datetime | None = None
    rejected_at: datetime | None = None
    expired_at: datetime | None = None
    disputed_at: datetime | None = None
    resolved_at: datetime | None = None
    cancelled_by: str | None = None
    cancellation_reason: str | None = None
    escrow_transaction_id: str | None = None
    settlement_transaction_id: str | None = None
    created_at: datetime | None = None
    updated_at: datetime | None = None


# ── Review ────────────────────────────────────────────────────────────

class Review(BaseModel):
    id: str
    trade_id: str
    listing_id: str
    reviewer_agent_id: str
    reviewed_agent_id: str
    rating: int
    body: str | None = None
    created_at: datetime | None = None


# ── Wallet ────────────────────────────────────────────────────────────

class Wallet(BaseModel):
    id: str
    agent_id: str
    balance: Decimal
    held_balance: Decimal
    available_balance: Decimal
    currency: str = "CREDIT"
    version: int = 0
    created_at: datetime | None = None


class LedgerEntry(BaseModel):
    id: str
    transaction_id: str
    entry_type: str
    amount: Decimal
    balance_after: Decimal
    category: str
    reference_type: str | None = None
    reference_id: str | None = None
    description: str | None = None
    created_at: datetime | None = None


# ── Pagination ────────────────────────────────────────────────────────

class Page(BaseModel):
    items: list[Any]
    total: int
    offset: int
    limit: int
    has_more: bool


# ── Platform Stats ────────────────────────────────────────────────────

class PlatformStats(BaseModel):
    total_agents: int
    active_listings: int
    total_trades: int
    total_volume: Decimal
    trades_by_type: dict[str, int] = {}
    recent_activity: list[dict[str, Any]] = []
