from __future__ import annotations

from typing import Any

from pydantic import AliasChoices, BaseModel, ConfigDict, Field


class ProdutoCatalogoBase(BaseModel):
    model_config = ConfigDict(populate_by_name=True)

    codigo_barras: str = Field(
        min_length=1,
        max_length=255,
        validation_alias=AliasChoices("codigo_ean", "codigo_barras"),
    )
    codigo_interno: str | None = Field(default=None, max_length=100)
    nome: str = Field(min_length=1, max_length=500)
    descricao: str | None = Field(default=None, max_length=5000)
    preco_consumidor: float = Field(
        ge=0,
        validation_alias=AliasChoices("preco_base", "preco_consumidor"),
    )
    preco_fabrica: float | None = Field(default=None, ge=0)
    pmc: float | None = Field(default=None, ge=0)
    desconto_comercial: float | None = Field(default=0, ge=0, le=100)
    valor_substituicao_tributaria: float | None = Field(default=None, ge=0)
    preco_nf: float | None = Field(default=None, ge=0)
    laboratorioId: str | None = Field(
        default=None,
        validation_alias=AliasChoices("laboratorio", "laboratorioId"),
    )
    categoriaId: str | None = Field(
        default=None,
        validation_alias=AliasChoices("categoria", "categoriaId"),
    )
    lab_nome: str | None = Field(default=None, max_length=255)
    cat_nome: str | None = Field(default=None, max_length=255)
    subcategoriaId: str | None = Field(
        default=None,
        validation_alias=AliasChoices("subcategoria", "subcategoriaId"),
    )


class ProdutoCatalogoCreate(ProdutoCatalogoBase):
    pass


class ProdutoCatalogoUpdate(BaseModel):
    model_config = ConfigDict(populate_by_name=True)

    codigo_barras: str | None = Field(
        default=None,
        min_length=1,
        max_length=255,
        validation_alias=AliasChoices("codigo_ean", "codigo_barras"),
    )
    codigo_interno: str | None = Field(default=None, max_length=100)
    nome: str | None = Field(default=None, min_length=1, max_length=500)
    descricao: str | None = Field(default=None, max_length=5000)
    preco_consumidor: float | None = Field(
        default=None,
        ge=0,
        validation_alias=AliasChoices("preco_base", "preco_consumidor"),
    )
    preco_fabrica: float | None = Field(default=None, ge=0)
    pmc: float | None = Field(default=None, ge=0)
    desconto_comercial: float | None = Field(default=None, ge=0, le=100)
    valor_substituicao_tributaria: float | None = Field(default=None, ge=0)
    preco_nf: float | None = Field(default=None, ge=0)
    laboratorioId: str | None = Field(
        default=None,
        validation_alias=AliasChoices("laboratorio", "laboratorioId"),
    )
    categoriaId: str | None = Field(
        default=None,
        validation_alias=AliasChoices("categoria", "categoriaId"),
    )
    lab_nome: str | None = Field(default=None, max_length=255)
    cat_nome: str | None = Field(default=None, max_length=255)
    subcategoriaId: str | None = Field(
        default=None,
        validation_alias=AliasChoices("subcategoria", "subcategoriaId"),
    )


class ProdutoCatalogoOut(BaseModel):
    id: str
    collectionId: str | None = None
    codigo_barras: str
    codigo_interno: str | None
    nome: str
    descricao: str | None
    preco_consumidor: float
    preco_fabrica: float | None
    pmc: float | None
    desconto_comercial: float | None
    valor_substituicao_tributaria: float | None
    preco_nf: float | None
    laboratorioId: str | None
    categoriaId: str | None
    lab_nome: str | None = None
    cat_nome: str | None = None
    subcategoriaId: str | None
    createdAt: str
    updatedAt: str

    model_config = ConfigDict(from_attributes=True)


class ProdutoCatalogoPublicOut(BaseModel):
    model_config = ConfigDict(populate_by_name=True)

    id: str | None = Field(default=None, alias="$id")
    codigo_ean: str = Field(min_length=1, max_length=255)
    codigo_interno: str | None = Field(default=None, max_length=100)
    nome: str = Field(min_length=1, max_length=500)
    descricao: str | None = Field(default=None, max_length=5000)
    preco_base: float = Field(ge=0)
    preco_fabrica: float | None = Field(default=None, ge=0)
    pmc: float | None = Field(default=None, ge=0)
    desconto_comercial: float | None = Field(default=None, ge=0, le=100)
    valor_substituicao_tributaria: float | None = Field(default=None, ge=0)
    preco_nf: float | None = Field(default=None, ge=0)
    collection_id: str | None = None
    laboratorio: str | None = None
    categoria: str | None = None
    lab_nome: str | None = None
    cat_nome: str | None = None
    subcategoria: str | None = None


class ProdutoCatalogoDocumentOut(BaseModel):
    id: str = Field(alias="$id")
    sequence: int | None = Field(default=None, alias="$sequence")
    database_id: str | None = Field(default=None, alias="$databaseId")
    collection_id: str | None = Field(default=None, alias="$collectionId")
    created_at: str = Field(alias="$createdAt")
    updated_at: str = Field(alias="$updatedAt")
    permissions: list[str] = Field(default_factory=list, alias="$permissions")
    codigo_ean: str
    codigo_interno: str | None = None
    nome: str
    descricao: str | None = None
    preco_base: float
    preco_fabrica: float | None = None
    pmc: float | None = None
    desconto_comercial: float | None = None
    valor_substituicao_tributaria: float | None = None
    preco_nf: float | None = None
    laboratorio: str | None = None
    categoria: str | None = None
    lab_nome: str | None = None
    cat_nome: str | None = None
    subcategoria: str | None = None

    model_config = ConfigDict(populate_by_name=True)


class ProdutosCatalogoDocumentsResponse(BaseModel):
    total: int = Field(ge=0)
    documents: list[ProdutoCatalogoDocumentOut]


class ProdutoCatalogoAppwriteDocument(BaseModel):
    id: str = Field(alias="$id")
    permissions: list[str] = Field(default_factory=list, alias="$permissions")
    created_at: str = Field(alias="$createdAt")
    updated_at: str = Field(alias="$updatedAt")
    sequence: int | None = Field(default=None, alias="$sequence")
    database_id: str | None = Field(default=None, alias="$databaseId")
    collection_id: str | None = Field(default=None, alias="$collectionId")
    codigo_ean: str
    codigo_interno: str | None = None
    nome: str
    descricao: str | None = None
    preco_base: float = Field(ge=0)
    preco_fabrica: float | None = Field(default=None, ge=0)
    pmc: float | None = Field(default=None, ge=0)
    desconto_comercial: float | None = Field(default=None, ge=0, le=100)
    valor_substituicao_tributaria: float | None = Field(default=None, ge=0)
    preco_nf: float | None = Field(default=None, ge=0)
    laboratorio: Any | None = None
    categoria: Any | None = None
    subcategoria: Any | None = None

    model_config = ConfigDict(populate_by_name=True, extra="allow")


class ProdutoCatalogoCreateRequest(BaseModel):
    document_id: str = Field(default="unique()", alias="documentId")
    data: ProdutoCatalogoCreate
    permissions: list[str] | None = Field(default=None, alias="permissions")

    model_config = ConfigDict(populate_by_name=True)


class ProdutoCatalogoUpdateRequest(BaseModel):
    data: ProdutoCatalogoUpdate
    permissions: list[str] | None = Field(default=None, alias="permissions")

    model_config = ConfigDict(populate_by_name=True)
