"""API request and response schemas.

Pydantic schemas enforce input validation and define the API shape
returned to external callers.
"""

from datetime import datetime
from typing import Optional

from pydantic import BaseModel, EmailStr, Field, ConfigDict


class CheckoutSessionCreate(BaseModel):
    """Payload for creating a checkout session.

    This payload is provided by partner websites when they initiate a
    payment attempt for a specific customer order.
    """

    partner_id: Optional[str] = None
    external_order_id: str
    amount: float = Field(gt=0)
    currency: str = Field(min_length=3, max_length=3)
    success_url: Optional[str] = None
    cancel_url: Optional[str] = None
    customer_email: Optional[EmailStr] = None
    customer_name: Optional[str] = None
    customer_country: Optional[str] = None
    service_type: Optional[str] = None
    channel: Optional[str] = None
    consent_analytics: Optional[bool] = False
    consent_marketing: Optional[bool] = False


class CheckoutSessionOut(BaseModel):
    """Response model for checkout sessions.

    Returned after creation and when fetching session status.
    """

    model_config = ConfigDict(from_attributes=True)

    id: str
    partner_id: str
    external_order_id: str
    amount: float
    currency: str
    status: str
    success_url: Optional[str] = None
    cancel_url: Optional[str] = None
    customer_email: Optional[str] = None
    customer_name: Optional[str] = None
    customer_country: Optional[str] = None
    service_type: Optional[str] = None
    channel: Optional[str] = None
    payout_status: str
    payout_method: Optional[str] = None
    payout_batch_id: Optional[str] = None
    payout_paid_at: Optional[datetime] = None
    consent_analytics: bool
    consent_marketing: bool
    created_at: datetime
    updated_at: datetime


class WebhookEventIn(BaseModel):
    """Incoming webhook payload from the payment processor."""

    event_id: str
    session_id: str
    status: str


class PayoutUpdateIn(BaseModel):
    """Payload for updating payout status for a session."""

    payout_status: str
    payout_method: Optional[str] = None
    payout_batch_id: Optional[str] = None
    payout_paid_at: Optional[datetime] = None


class PartnerCreate(BaseModel):
    """Payload for creating a partner record (internal/admin use)."""

    name: str
    callback_url: Optional[str] = None


class PartnerOut(BaseModel):
    """Response model for partner records."""

    model_config = ConfigDict(from_attributes=True)

    id: str
    name: str
    callback_url: Optional[str] = None
    created_at: datetime


class PartnerCreatedOut(PartnerOut):
    """Partner response that includes the generated API key (one-time view)."""

    api_key: str
