from datetime import datetime
from decimal import Decimal

from pydantic import BaseModel, ConfigDict, EmailStr, Field


class LeadCreate(BaseModel):
    name: str = Field(..., min_length=1, max_length=255)
    contact_name: str | None = Field(None, max_length=160)
    email: EmailStr | None = None
    phone: str | None = Field(None, max_length=40)
    company_name: str | None = Field(None, max_length=255)
    source: str | None = Field(None, max_length=60)
    preferred_contact_method: str | None = Field(None, max_length=20)
    status: str = Field("new", max_length=20)
    estimated_value: Decimal | None = Field(None, ge=0, max_digits=14, decimal_places=2)
    owner_user_id: str | None = Field(None, max_length=64)
    notes: str | None = None


class LeadUpdate(BaseModel):
    name: str | None = Field(None, min_length=1, max_length=255)
    contact_name: str | None = Field(None, max_length=160)
    email: EmailStr | None = None
    phone: str | None = Field(None, max_length=40)
    company_name: str | None = Field(None, max_length=255)
    source: str | None = Field(None, max_length=60)
    preferred_contact_method: str | None = Field(None, max_length=20)
    status: str | None = Field(None, max_length=20)
    estimated_value: Decimal | None = Field(None, ge=0, max_digits=14, decimal_places=2)
    owner_user_id: str | None = Field(None, max_length=64)
    notes: str | None = None


class LeadConvert(BaseModel):
    """Parámetros de conversión de un prospecto."""

    create_opportunity: bool = True
    opportunity_name: str | None = Field(None, max_length=255)
    pipeline_id: int | None = None
    stage_id: int | None = None
    amount: Decimal | None = Field(None, ge=0, max_digits=14, decimal_places=2)


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

    id: int
    name: str
    contact_name: str | None
    email: str | None
    phone: str | None
    company_name: str | None
    source: str | None
    preferred_contact_method: str | None = None
    status: str
    estimated_value: Decimal | None
    owner_user_id: str | None
    converted_account_id: int | None
    converted_contact_id: int | None
    converted_opportunity_id: int | None
    notes: str | None
    tenant_id: int
    company_id: int
    created_at: datetime
    updated_at: datetime


class LeadConvertResult(BaseModel):
    lead: LeadResponse
    account_id: int
    contact_id: int | None
    opportunity_id: int | None
