"""Driver DTOs — Phase 10.1."""
from __future__ import annotations

from datetime import date, datetime
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field

from app.models.types import (
    DriverCredentialKind,
    DriverDocumentKind,
    DriverLifecycleAction,
    DriverStatus,
)
from app.schemas.common import ORMBase

_FORBID = ConfigDict(extra="forbid")


class DriverCreate(BaseModel):
    model_config = _FORBID

    organization_id: UUID
    hub_id: UUID | None = None
    code: str = Field(max_length=50)
    display_name: str = Field(max_length=255)
    first_name: str | None = Field(default=None, max_length=100)
    last_name: str | None = Field(default=None, max_length=100)
    mobile: str | None = Field(default=None, max_length=32)
    email: str | None = Field(default=None, max_length=255)
    status: DriverStatus = DriverStatus.PENDING
    external_user_ref: str | None = Field(default=None, max_length=100)
    external_crm_contact_ref: str | None = Field(default=None, max_length=100)
    hire_date: date | None = None
    notes: str | None = None
    capability_tags: list[str] | None = None
    metadata_json: dict | None = None


class DriverUpdate(BaseModel):
    model_config = _FORBID

    hub_id: UUID | None = None
    display_name: str | None = Field(default=None, max_length=255)
    first_name: str | None = Field(default=None, max_length=100)
    last_name: str | None = Field(default=None, max_length=100)
    mobile: str | None = Field(default=None, max_length=32)
    email: str | None = Field(default=None, max_length=255)
    external_user_ref: str | None = Field(default=None, max_length=100)
    external_crm_contact_ref: str | None = Field(default=None, max_length=100)
    hire_date: date | None = None
    notes: str | None = None
    capability_tags: list[str] | None = None
    metadata_json: dict | None = None
    version: int = Field(ge=1)


class DriverRead(ORMBase):
    id: UUID
    tenant_id: UUID
    organization_id: UUID
    hub_id: UUID | None
    code: str
    display_name: str
    first_name: str | None
    last_name: str | None
    mobile: str | None
    email: str | None
    status: DriverStatus
    external_user_ref: str | None
    external_crm_contact_ref: str | None
    hire_date: date | None
    notes: str | None
    capability_tags: list | None
    metadata_json: dict | None
    status_reason: str | None
    status_changed_at: datetime | None
    version: int
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class DriverLifecycleRequest(BaseModel):
    model_config = _FORBID

    reason: str | None = Field(default=None, max_length=500)
    version: int = Field(ge=1)


class DriverLifecycleEventRead(ORMBase):
    id: UUID
    tenant_id: UUID
    driver_id: UUID
    action: DriverLifecycleAction
    from_status: DriverStatus
    to_status: DriverStatus
    reason: str | None
    actor_user_id: str | None
    metadata_json: dict | None
    created_at: datetime


class DriverCredentialCreate(BaseModel):
    model_config = _FORBID

    kind: DriverCredentialKind = DriverCredentialKind.LICENSE
    number: str = Field(max_length=100)
    issued_at: date | None = None
    expires_at: date | None = None
    is_verified: bool = False
    document_file_ref: str | None = Field(default=None, max_length=255)
    metadata_json: dict | None = None


class DriverCredentialRead(ORMBase):
    id: UUID
    tenant_id: UUID
    driver_id: UUID
    kind: DriverCredentialKind
    number: str
    issued_at: date | None
    expires_at: date | None
    is_verified: bool
    document_file_ref: str | None
    metadata_json: dict | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class DriverDocumentCreate(BaseModel):
    model_config = _FORBID

    kind: DriverDocumentKind = DriverDocumentKind.OTHER
    title: str = Field(max_length=255)
    storage_file_ref: str = Field(max_length=255)
    notes: str | None = None


class DriverDocumentRead(ORMBase):
    id: UUID
    tenant_id: UUID
    driver_id: UUID
    kind: DriverDocumentKind
    title: str
    storage_file_ref: str
    notes: str | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class DriverListResponse(BaseModel):
    items: list[DriverRead]
    total: int
    page: int
    page_size: int
