from __future__ import annotations

from typing import Literal, Optional

from pydantic import AliasChoices, BaseModel, EmailStr, Field
from pydantic.config import ConfigDict


NivelUsuario = Literal["superadmin", "admin", "colaborador", "entregador"]
NivelRegistravel = Literal["admin", "colaborador", "entregador"]


class RegisterIn(BaseModel):
    identificador: str = Field(
        min_length=1,
        max_length=255,
        description="Login único do usuário",
    )
    email: EmailStr = Field(description="E-mail do usuário")
    nome: str = Field(
        min_length=1,
        max_length=255,
        description="Nome do usuário",
    )
    senha: str = Field(
        min_length=6,
        max_length=255,
        description="Senha do usuário",
    )
    ativo: Optional[bool] = Field(default=True, description="Default true")
    nivel: Optional[NivelRegistravel] = Field(
        default=None,
        description="Default 'admin'",
    )
    superadmin: Optional[bool] = Field(
        default=False,
        description="Ignorado; backend força False",
    )
    auth_id_appwrite: Optional[str] = Field(
        default=None,
        alias="auth-id-appwrite",
        serialization_alias="auth-id-appwrite",
        validation_alias=AliasChoices("auth-id-appwrite", "auth_id_appwrite"),
        description="Identificador do usuário na instância do Appwrite.",
        max_length=255,
    )
    registro_completo: Optional[bool] = Field(
        default=False,
        alias="registro-completo",
        serialization_alias="registro-completo",
        validation_alias=AliasChoices("registro-completo", "registro_completo"),
        description="Indica se o usuário completou o registro.",
    )

    model_config = ConfigDict(
        populate_by_name=True,
        json_schema_extra={
            "example": {
                "identificador": "tiago",
                "email": "tiago.silva@empresa.com",
                "nome": "Tiago Silva",
                "senha": "Minhasenha@123",
                "ativo": True,
                "nivel": "admin",
                "superadmin": False,
                "auth-id-appwrite": "user_1234567890",
                "registro-completo": False,
            }
        }
    )


class LoginIn(BaseModel):
    identificador: str
    senha: str


class AuthOut(BaseModel):
    access_token: str
    token_type: str = "bearer"
    user: MeOut


class MeOut(BaseModel):
    id: str
    identificador: str
    ativo: bool
    superadmin: bool
    auth_id_appwrite: str | None = Field(
        default=None,
        alias="auth-id-appwrite",
        serialization_alias="auth-id-appwrite",
        validation_alias=AliasChoices("auth-id-appwrite", "auth_id_appwrite"),
    )
    nome: str | None = None
    email: str | None = None
    nivel: str | None = "colaborador"
    registro_completo: bool = Field(
        default=False,
        alias="registro-completo",
        serialization_alias="registro-completo",
        validation_alias=AliasChoices("registro-completo", "registro_completo"),
        description="Indica se o usuário completou o registro.",
    )
    enderecos: list[str] = Field(default_factory=list)
    empresas_dados: list[str] = Field(
        default_factory=list,
        alias="empresasDados",
        serialization_alias="empresasDados",
        validation_alias=AliasChoices("empresasDados", "empresas_dados"),
    )
    nome_social: str | None = Field(
        default=None,
        alias="nome-social",
        serialization_alias="nome-social",
        validation_alias=AliasChoices("nome-social", "nome_social"),
    )
    cpf: str | None = None
    createdAt: str
    updatedAt: str

    model_config = ConfigDict(
        populate_by_name=True,
        json_schema_extra={
            "example": {
                "id": "68ded4a25207fbccf6e4",
                "identificador": "andre.fr93@gmail.com",
                "ativo": True,
                "superadmin": True,
                "auth-id-appwrite": "string",
                "nome": "NANDO",
                "email": "andre.fr93@gmail.com",
                "nivel": "superadmin",
                "registro-completo": True,
                "enderecos": ["68e7bf322aea078d2a05"],
                "empresasDados": ["68e7bf32badbe173fd86"],
                "nome-social": "N/A",
                "cpf": "41181521858",
                "createdAt": "2025-10-02T19:38:10.336+00:00",
                "updatedAt": "2025-10-09T13:57:07.833+00:00",
            }
        },
    )
