from datetime import date, datetime
from decimal import Decimal

from pydantic import BaseModel, ConfigDict, Field


class ServiceRequestBase(BaseModel):
    reference: str | None = Field(None, max_length=40)
    account_id: int | None = None
    opportunity_id: int | None = None
    operation_type: str = Field(..., max_length=20)  # importacion | exportacion
    transport_mode: str | None = Field(None, max_length=20)
    service_type: str | None = Field(None, max_length=20)
    incoterm: str | None = Field(None, max_length=10)
    origin: str | None = Field(None, max_length=160)
    destination: str | None = Field(None, max_length=160)
    cargo_type: str | None = Field(None, max_length=120)
    weight: Decimal | None = Field(None, ge=0, max_digits=14, decimal_places=3)
    volume: Decimal | None = Field(None, ge=0, max_digits=14, decimal_places=3)
    load_type: str | None = Field(None, max_length=10)
    container_equipment: str | None = Field(None, max_length=120)
    commodity: str | None = None
    required_date: date | None = None
    destination_agent_id: int | None = None
    requirements: str | None = None
    status: str = Field("nueva", max_length=20)
    notes: str | None = None
    owner_user_id: str | None = Field(None, max_length=64)


class ServiceRequestCreate(ServiceRequestBase):
    pass


class ServiceRequestContactInput(BaseModel):
    """Registro del contacto al cliente como etapa del flujo comercial (R-C-04)."""
    notes: str | None = None


class ServiceRequestFromOpportunityInput(BaseModel):
    """Datos para convertir una oportunidad del embudo en solicitud/RFQ (R-C-02)."""
    operation_type: str = Field(..., max_length=20)  # importacion | exportacion
    transport_mode: str | None = Field(None, max_length=20)
    service_type: str | None = Field(None, max_length=20)
    incoterm: str | None = Field(None, max_length=10)
    origin: str | None = Field(None, max_length=160)
    destination: str | None = Field(None, max_length=160)
    notes: str | None = None


class ServiceRequestUpdate(BaseModel):
    reference: str | None = Field(None, max_length=40)
    account_id: int | None = None
    opportunity_id: int | None = None
    operation_type: str | None = Field(None, max_length=20)
    transport_mode: str | None = Field(None, max_length=20)
    service_type: str | None = Field(None, max_length=20)
    incoterm: str | None = Field(None, max_length=10)
    origin: str | None = Field(None, max_length=160)
    destination: str | None = Field(None, max_length=160)
    cargo_type: str | None = Field(None, max_length=120)
    weight: Decimal | None = Field(None, ge=0, max_digits=14, decimal_places=3)
    volume: Decimal | None = Field(None, ge=0, max_digits=14, decimal_places=3)
    load_type: str | None = Field(None, max_length=10)
    container_equipment: str | None = Field(None, max_length=120)
    commodity: str | None = None
    required_date: date | None = None
    destination_agent_id: int | None = None
    requirements: str | None = None
    status: str | None = Field(None, max_length=20)
    notes: str | None = None
    owner_user_id: str | None = Field(None, max_length=64)


class ServiceRequestResponse(ServiceRequestBase):
    model_config = ConfigDict(from_attributes=True)

    id: int
    first_contact_at: datetime | None = None
    first_contact_notes: str | None = None
    tenant_id: int
    company_id: int
    created_by: str | None = None
    updated_by: str | None = None
    created_at: datetime
    updated_at: datetime


class RateRequestBase(BaseModel):
    service_request_id: int
    supplier_id: int | None = None
    concept: str = Field(..., max_length=60)
    description: str | None = Field(None, max_length=255)
    status: str = Field("solicitada", max_length=20)
    rate_amount: Decimal | None = Field(None, ge=0, max_digits=14, decimal_places=2)
    currency: str | None = Field(None, max_length=3)
    valid_until: date | None = None
    notes: str | None = None


class RateRequestCreate(RateRequestBase):
    pass


class RateRequestUpdate(BaseModel):
    supplier_id: int | None = None
    concept: str | None = Field(None, max_length=60)
    description: str | None = Field(None, max_length=255)
    status: str | None = Field(None, max_length=20)
    rate_amount: Decimal | None = Field(None, ge=0, max_digits=14, decimal_places=2)
    currency: str | None = Field(None, max_length=3)
    valid_until: date | None = None
    notes: str | None = None


class RateRequestResponse(RateRequestBase):
    model_config = ConfigDict(from_attributes=True)

    id: int
    tenant_id: int
    company_id: int
    created_at: datetime
    updated_at: datetime
