"""Delivery integration DTOs — Phase 13.7."""
from __future__ import annotations

from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field

from app.models.types import DeliveryJobStatus, LifecycleStatus
from app.schemas.common import ORMBase

_FORBID = ConfigDict(extra="forbid")


class DeliveryIntegrationCreate(BaseModel):
    model_config = _FORBID
    clinic_id: UUID | None = None
    pharmacy_id: UUID | None = None
    code: str = Field(max_length=50)
    name: str = Field(max_length=255)
    delivery_merchant_ref: str = Field(max_length=255)
    webhook_secret_ref: str | None = Field(default=None, max_length=255)
    status: LifecycleStatus = LifecycleStatus.ACTIVE
    config: dict | None = None


class DeliveryIntegrationRead(ORMBase):
    id: UUID
    tenant_id: UUID
    clinic_id: UUID | None
    pharmacy_id: UUID | None
    code: str
    name: str
    delivery_merchant_ref: str
    webhook_secret_ref: str | None
    status: LifecycleStatus
    config: dict | None
    version: int
    is_deleted: bool
    created_at: datetime
    updated_at: datetime


class DeliveryJobIntentCreate(BaseModel):
    model_config = _FORBID
    registration_id: UUID
    idempotency_key: str = Field(max_length=100)
    code: str = Field(max_length=50)
    pickup_address: dict | None = None
    dropoff_address: dict | None = None
    metadata_json: dict | None = None


class DeliveryJobIntentRead(ORMBase):
    id: UUID
    tenant_id: UUID
    registration_id: UUID
    prescription_order_id: UUID
    code: str
    delivery_job_ref: str | None
    status: DeliveryJobStatus
    idempotency_key: str
    pickup_address: dict | None
    dropoff_address: dict | None
    metadata_json: dict | None
    version: int
    is_deleted: bool
    created_at: datetime
    updated_at: datetime


class DeliveryStatusWebhook(BaseModel):
    model_config = _FORBID
    delivery_job_ref: str = Field(max_length=255)
    status: DeliveryJobStatus
    external_status: str | None = Field(default=None, max_length=100)
    payload: dict | None = None
