from datetime import datetime

from pydantic import BaseModel, ConfigDict, Field


class WhatsAppStatusResponse(BaseModel):
    venue_id: int
    venue_name: str
    configured: bool
    phone_number_id_masked: str | None
    phone_number_id: str
    business_account_id: str
    business_id: str
    display_phone_number: str | None
    verified_name: str | None
    access_token_configured: bool
    phone_number_id_saved: bool
    access_token_saved: bool
    uses_global_fallback: bool
    verify_token_configured: bool
    app_secret_configured: bool
    graph_api_version: str
    webhook_path: str
    webhook_url: str
    message: str


class WhatsAppConfigValidationResponse(BaseModel):
    success: bool
    phone_number_id: str
    display_phone_number: str | None = None
    verified_name: str | None = None
    detail: str


class WhatsAppSendTestRequest(BaseModel):
    to: str = Field(min_length=6, max_length=32)
    message: str = Field(min_length=1, max_length=4096)


class WhatsAppSendTestResponse(BaseModel):
    success: bool
    recipient: str
    message_id: str | None = None
    detail: str


class WhatsAppWebhookAck(BaseModel):
    received: bool
    processed_events: int


class WhatsAppEventLogRead(BaseModel):
    id: int
    event_type: str
    contact_phone: str | None
    wa_message_id: str | None
    summary: str
    payload: dict
    created_at: datetime

    model_config = ConfigDict(from_attributes=True)


class WhatsAppAssistantTurnRead(BaseModel):
    id: int
    role: str
    content: str
    assistant_route: str | None
    assistant_model: str | None
    source_wa_message_id: str | None
    trace: dict
    created_at: datetime

    model_config = ConfigDict(from_attributes=True)


class WhatsAppConversationSummaryRead(BaseModel):
    contact_phone: str
    turn_count: int
    last_turn_at: datetime
    last_role: str | None
    last_message: str | None
    last_user_message: str | None
    last_assistant_message: str | None
