"""
Pydantic models for authentication endpoints
Story 3.6: Documentation OpenAPI (Swagger + ReDoc)
"""

from pydantic import BaseModel, Field, EmailStr
from typing import Optional


class RegisterRequest(BaseModel):
    """Request model for user registration"""
    
    email: EmailStr = Field(
        ..., 
        example="utilisateur@exemple.com", 
        description="Adresse email de l'utilisateur"
    )
    password: str = Field(
        ..., 
        example="MotDePasse123!", 
        description="Mot de passe (min 8 caractères)",
        min_length=8
    )
    name: Optional[str] = Field(
        None,
        example="Jean Dupont",
        description="Nom complet de l'utilisateur (optionnel)"
    )

    class Config:
        json_schema_extra = {
            "example": {
                "email": "utilisateur@exemple.com",
                "password": "MotDePasse123!",
                "name": "Jean Dupont"
            }
        }


class LoginRequest(BaseModel):
    """Request model for user login"""
    
    email: EmailStr = Field(
        ..., 
        example="utilisateur@exemple.com", 
        description="Adresse email de l'utilisateur"
    )
    password: str = Field(
        ..., 
        example="MotDePasse123!", 
        description="Mot de passe"
    )

    class Config:
        json_schema_extra = {
            "example": {
                "email": "utilisateur@exemple.com",
                "password": "MotDePasse123!"
            }
        }


class TokenData(BaseModel):
    """Token data in response"""
    
    access_token: str = Field(
        ..., 
        example="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...", 
        description="Token d'accès JWT (expire dans 15 minutes)"
    )
    refresh_token: str = Field(
        ..., 
        example="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...", 
        description="Token de rafraîchissement JWT (expire dans 7 jours)"
    )
    token_type: str = Field(
        default="bearer", 
        example="bearer", 
        description="Type de token"
    )


class TokenResponse(BaseModel):
    """Response model for authentication tokens"""
    
    data: TokenData
    meta: dict = Field(default_factory=dict)

    class Config:
        json_schema_extra = {
            "example": {
                "data": {
                    "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
                    "refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
                    "token_type": "bearer"
                },
                "meta": {}
            }
        }


class LogoutResponse(BaseModel):
    """Response model for logout"""
    
    data: dict = Field(
        default_factory=lambda: {"message": "Déconnexion réussie"},
        description="Message de confirmation"
    )
    meta: dict = Field(default_factory=dict)

    class Config:
        json_schema_extra = {
            "example": {
                "data": {
                    "message": "Déconnexion réussie"
                },
                "meta": {}
            }
        }


class RefreshRequest(BaseModel):
    """Request model for token refresh"""
    
    refresh_token: str = Field(
        ..., 
        example="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...", 
        description="Token de rafraîchissement"
    )

    class Config:
        json_schema_extra = {
            "example": {
                "refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
            }
        }


class UserData(BaseModel):
    """User data in responses"""
    
    id: str = Field(..., description="Identifiant unique de l'utilisateur")
    email: str = Field(..., description="Adresse email")
    tier: str = Field(..., description="Niveau d'abonnement (free, pro, etc.)")


class RegisterResponse(BaseModel):
    """Response model for registration"""
    
    data: UserData
    meta: dict = Field(default_factory=dict)

    class Config:
        json_schema_extra = {
            "example": {
                "data": {
                    "id": "usr_abc123def456",
                    "email": "utilisateur@exemple.com",
                    "tier": "free"
                },
                "meta": {}
            }
        }