"""Pydantic schemas for the DHL shipping module."""

from __future__ import annotations

from typing import Any, Dict, List, Optional

from pydantic import BaseModel, Field


class DhlSettingsOut(BaseModel):
    """Safe settings payload returned to the admin UI (masked secrets)."""

    api_key: Optional[str] = None
    api_secret: Optional[str] = None
    account_number: Optional[str] = None
    environment: str = "sandbox"
    configured: bool = False


class DhlSettingsUpdate(BaseModel):
    """Admin-provided credential update. Fields left ``None`` are unchanged."""

    api_key: Optional[str] = Field(default=None, description="DHL MyDHL API key")
    api_secret: Optional[str] = Field(default=None, description="DHL MyDHL API secret")
    account_number: Optional[str] = Field(default=None, description="DHL shipper account number")
    environment: Optional[str] = Field(default=None, description="'sandbox' or 'production'")


class DhlTestResult(BaseModel):
    ok: bool
    stub: bool
    message: str


class DhlShipmentResult(BaseModel):
    order_id: int
    tracking_number: str
    label_url: Optional[str] = None
    stub: bool
    message: str


class DhlTrackingEvent(BaseModel):
    timestamp: Optional[str] = None
    description: str
    location: Optional[str] = None


class DhlTrackingResult(BaseModel):
    tracking_number: str
    status: str
    stub: bool
    message: str
    events: List[DhlTrackingEvent] = []
    order: Optional[Dict[str, Any]] = None


class DhlNotificationOut(BaseModel):
    """Single row from the ``shipment_notifications`` delivery log."""

    id: int
    order_id: Optional[int] = None
    order_number: Optional[str] = None
    tracking_number: str
    recipient_email: str
    previous_status: Optional[str] = None
    new_status: str
    event_description: Optional[str] = None
    event_location: Optional[str] = None
    event_timestamp: Optional[str] = None
    success: bool
    error_message: Optional[str] = None
    stub: bool
    created_at: Optional[str] = None


class DhlNotificationList(BaseModel):
    items: List[DhlNotificationOut]


class DhlPublicNotificationOut(BaseModel):
    """Safe, customer-facing notification log entry.

    Intentionally omits ``error_message`` and reveals only a masked form of
    the recipient email so the tracking page confirms *that* a status email
    went out to the customer, without leaking PII to anyone who happens to
    know the tracking number.
    """

    previous_status: Optional[str] = None
    new_status: str
    event_description: Optional[str] = None
    event_location: Optional[str] = None
    event_timestamp: Optional[str] = None
    sent: bool
    recipient_masked: Optional[str] = None
    created_at: Optional[str] = None


class DhlPublicNotificationList(BaseModel):
    items: List[DhlPublicNotificationOut]


class DhlNotificationCheckResult(BaseModel):
    """Outcome of the admin ``check-tracking`` action."""

    sent: bool
    reason: str
    status: Optional[str] = None
    previous_status: Optional[str] = None
    tracking_number: Optional[str] = None
    recipient: Optional[str] = None
    notification_id: Optional[int] = None
    error: Optional[str] = None
    stub: Optional[bool] = None