"""Routing & optimization DTOs — Phase 10.6."""
from __future__ import annotations

from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field

from app.models.types import OptimizationRunStatus, RoutePlanStatus, RouteStopStatus
from app.schemas.common import ORMBase

_FORBID = ConfigDict(extra="forbid")


class RoutePlanCreate(BaseModel):
    model_config = _FORBID

    organization_id: UUID
    code: str = Field(max_length=50)
    driver_id: UUID | None = None
    dispatch_job_ids: list[str] | None = None
    routing_engine_ref: str | None = Field(default=None, max_length=255)
    metadata_json: dict | None = None


class RoutePlanUpdate(BaseModel):
    model_config = _FORBID

    driver_id: UUID | None = None
    status: RoutePlanStatus | None = None
    dispatch_job_ids: list[str] | None = None
    routing_engine_ref: str | None = Field(default=None, max_length=255)
    metadata_json: dict | None = None
    version: int = Field(ge=1)


class RoutePlanRead(ORMBase):
    id: UUID
    tenant_id: UUID
    organization_id: UUID
    code: str
    status: RoutePlanStatus
    driver_id: UUID | None
    dispatch_job_ids: list | None
    routing_engine_ref: str | None
    planned_at: datetime | None
    metadata_json: dict | None
    version: int
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class RoutePlanListResponse(BaseModel):
    items: list[RoutePlanRead]
    total: int
    page: int
    page_size: int


class RouteStopCreate(BaseModel):
    model_config = _FORBID

    sequence: int = Field(ge=0)
    address_ref: str | None = Field(default=None, max_length=255)
    external_order_ref: str | None = Field(default=None, max_length=255)
    notes: str | None = None
    metadata_json: dict | None = None


class RouteStopRead(ORMBase):
    id: UUID
    tenant_id: UUID
    route_plan_id: UUID
    sequence: int
    status: RouteStopStatus
    address_ref: str | None
    external_order_ref: str | None
    notes: str | None
    metadata_json: dict | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class OptimizationRunCreate(BaseModel):
    model_config = _FORBID

    route_plan_id: UUID | None = None
    routing_engine_ref: str | None = Field(default=None, max_length=255)
    request_payload: dict | None = None


class OptimizationRunRead(ORMBase):
    id: UUID
    tenant_id: UUID
    route_plan_id: UUID | None
    status: OptimizationRunStatus
    routing_engine_ref: str | None
    request_payload: dict | None
    result_payload: dict | None
    started_at: datetime | None
    completed_at: datetime | None
    error_message: str | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class OptimizationRunListResponse(BaseModel):
    items: list[OptimizationRunRead]
    total: int
    page: int
    page_size: int
