"""Settlement DTOs — Phase 10.8."""
from __future__ import annotations

from datetime import datetime
from decimal import Decimal
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field

from app.models.types import SettlementIntentStatus, SettlementLineKind
from app.schemas.common import ORMBase

_FORBID = ConfigDict(extra="forbid")


class SettlementIntentCreate(BaseModel):
    model_config = _FORBID

    organization_id: UUID
    code: str = Field(max_length=50)
    driver_id: UUID | None = None
    dispatch_job_id: UUID | None = None
    total_amount: Decimal = Field(gt=0)
    currency_code: str = Field(default="IRR", max_length=3)
    notes: str | None = None
    metadata_json: dict | None = None


class SettlementIntentUpdate(BaseModel):
    model_config = _FORBID

    driver_id: UUID | None = None
    dispatch_job_id: UUID | None = None
    total_amount: Decimal | None = Field(default=None, gt=0)
    currency_code: str | None = Field(default=None, max_length=3)
    notes: str | None = None
    metadata_json: dict | None = None
    version: int = Field(ge=1)


class SettlementActionRequest(BaseModel):
    model_config = _FORBID

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


class SettlementIntentRead(ORMBase):
    id: UUID
    tenant_id: UUID
    organization_id: UUID
    code: str
    status: SettlementIntentStatus
    driver_id: UUID | None
    dispatch_job_id: UUID | None
    external_settlement_ref: str | None
    total_amount: Decimal
    currency_code: str
    notes: str | 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 SettlementIntentListResponse(BaseModel):
    items: list[SettlementIntentRead]
    total: int
    page: int
    page_size: int


class SettlementLineCreate(BaseModel):
    model_config = _FORBID

    kind: SettlementLineKind
    description: str | None = Field(default=None, max_length=255)
    amount: Decimal = Field(gt=0)
    currency_code: str = Field(default="IRR", max_length=3)
    metadata_json: dict | None = None


class SettlementLineRead(ORMBase):
    id: UUID
    tenant_id: UUID
    settlement_intent_id: UUID
    kind: SettlementLineKind
    description: str | None
    amount: Decimal
    currency_code: str
    metadata_json: dict | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime
