from datetime import date, datetime
from decimal import Decimal

from pydantic import BaseModel, ConfigDict, Field


class OpportunityCreate(BaseModel):
    name: str = Field(..., min_length=1, max_length=255)
    account_id: int | None = None
    contact_id: int | None = None
    pipeline_id: int | None = None
    stage_id: int | None = None
    amount: Decimal | None = Field(None, ge=0, max_digits=14, decimal_places=2)
    currency: str = Field("MXN", max_length=3)
    probability: int | None = Field(None, ge=0, le=100)
    expected_close_date: date | None = None
    source: str | None = Field(None, max_length=60)
    owner_user_id: str | None = Field(None, max_length=64)
    notes: str | None = None
    operation_type: str | None = Field(None, max_length=20)  # importacion | exportacion


class OpportunityUpdate(BaseModel):
    name: str | None = Field(None, min_length=1, max_length=255)
    account_id: int | None = None
    contact_id: int | None = None
    pipeline_id: int | None = None
    stage_id: int | None = None
    amount: Decimal | None = Field(None, ge=0, max_digits=14, decimal_places=2)
    currency: str | None = Field(None, max_length=3)
    probability: int | None = Field(None, ge=0, le=100)
    status: str | None = Field(None, max_length=20)
    expected_close_date: date | None = None
    won_date: date | None = None
    lost_date: date | None = None
    lost_reason: str | None = Field(None, max_length=255)
    source: str | None = Field(None, max_length=60)
    owner_user_id: str | None = Field(None, max_length=64)
    notes: str | None = None
    operation_type: str | None = Field(None, max_length=20)


class OpportunityMove(BaseModel):
    """Mueve la oportunidad a otra etapa (drag & drop del Kanban)."""

    stage_id: int


class OpportunityResponse(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: int
    name: str
    account_id: int | None
    contact_id: int | None
    pipeline_id: int | None
    stage_id: int | None
    amount: Decimal | None
    currency: str
    probability: int | None
    status: str
    expected_close_date: date | None
    closed_at: datetime | None
    won_date: date | None = None
    lost_date: date | None = None
    lost_reason: str | None
    source: str | None
    owner_user_id: str | None
    notes: str | None
    operation_type: str | None = None
    reference: str | None = None
    case_id: int | None = None
    converted_service_request_id: int | None = None
    tenant_id: int
    company_id: int
    created_at: datetime
    updated_at: datetime
