"""
Common Pydantic models for API documentation
Story 3.6: Documentation OpenAPI (Swagger + ReDoc)
"""

from pydantic import BaseModel, Field
from typing import Optional, Dict, Any, List


class SuccessResponse(BaseModel):
    """Generic success response"""
    
    data: Dict[str, Any] = Field(..., description="Données de réponse")
    meta: Dict[str, Any] = Field(default_factory=dict, description="Métadonnées")

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


class HealthCheckResponse(BaseModel):
    """Response model for health check endpoint"""
    
    status: str = Field(..., description="Statut de santé (healthy/unhealthy)")
    translation_service: str = Field(..., description="Service de traduction configuré")
    database: Dict[str, Any] = Field(..., description="Statut de la base de données")
    redis: Dict[str, Any] = Field(..., description="Statut de Redis")
    memory: Dict[str, Any] = Field(..., description="Informations mémoire")
    disk: Dict[str, Any] = Field(..., description="Informations disque")
    cleanup_service: Dict[str, Any] = Field(..., description="Statut du service de cleanup")
    rate_limits: Dict[str, Any] = Field(..., description="Configuration des rate limits")
    translation_cache: Dict[str, Any] = Field(..., description="Statut du cache de traduction")

    class Config:
        json_schema_extra = {
            "example": {
                "status": "healthy",
                "translation_service": "google",
                "database": {
                    "status": "healthy"
                },
                "redis": {
                    "status": "not_configured"
                },
                "memory": {},
                "disk": {},
                "cleanup_service": {
                    "running": True
                },
                "rate_limits": {
                    "requests_per_minute": 30,
                    "translations_per_minute": 10
                },
                "translation_cache": {
                    "hits": 1500,
                    "misses": 500,
                    "size": 42
                }
            }
        }


class ReadyCheckResponse(BaseModel):
    """Response model for readiness check endpoint"""
    
    ready: bool = Field(..., description="Si le service est prêt à recevoir du trafic")
    issues: Optional[List[str]] = Field(
        None, 
        description="Liste des problèmes si non prêt"
    )

    class Config:
        json_schema_extra = {
            "examples": [
                {
                    "summary": "Service prêt",
                    "value": {
                        "ready": True
                    }
                },
                {
                    "summary": "Service non prêt",
                    "value": {
                        "ready": False,
                        "issues": ["database_unavailable", "redis_unavailable"]
                    }
                }
            ]
        }


class RootResponse(BaseModel):
    """Response model for root endpoint"""
    
    name: str = Field(..., description="Nom de l'API")
    version: str = Field(..., description="Version de l'API")
    status: str = Field(..., description="Statut opérationnel")
    docs: str = Field(..., description="URL de la documentation Swagger")
    redoc: str = Field(..., description="URL de la documentation ReDoc")
    api_base: str = Field(..., description="Base URL des endpoints API")
    supported_formats: List[str] = Field(..., description="Formats de fichier supportés")

    class Config:
        json_schema_extra = {
            "example": {
                "name": "Office Translator API",
                "version": "1.0.0",
                "status": "operational",
                "docs": "/docs",
                "redoc": "/redoc",
                "api_base": "/api/v1",
                "supported_formats": [".xlsx", ".docx", ".pptx"]
            }
        }


class RateLimitStatusResponse(BaseModel):
    """Response model for rate limit status"""
    
    client_ip: str = Field(..., description="Adresse IP du client")
    limits: Dict[str, int] = Field(..., description="Limites configurées")
    current_usage: Dict[str, Any] = Field(..., description="Utilisation actuelle")

    class Config:
        json_schema_extra = {
            "example": {
                "client_ip": "192.168.1.1",
                "limits": {
                    "requests_per_minute": 30,
                    "requests_per_hour": 200,
                    "translations_per_minute": 10,
                    "translations_per_hour": 50
                },
                "current_usage": {
                    "requests_this_minute": 5,
                    "requests_this_hour": 42,
                    "translations_this_minute": 1,
                    "translations_this_hour": 8
                }
            }
        }


class MetricsResponse(BaseModel):
    """Response model for metrics endpoint"""
    
    system: Dict[str, Any] = Field(..., description="Métriques système")
    cleanup: Dict[str, Any] = Field(..., description="Métriques de cleanup")
    rate_limits: Dict[str, Any] = Field(..., description="Métriques de rate limiting")
    config: Dict[str, Any] = Field(..., description="Configuration actuelle")

    class Config:
        json_schema_extra = {
            "example": {
                "system": {
                    "memory": {},
                    "disk": {},
                    "status": "healthy"
                },
                "cleanup": {
                    "files_cleaned": 12,
                    "last_cleanup": "2024-01-15T10:00:00Z"
                },
                "rate_limits": {
                    "active_clients": 3
                },
                "config": {
                    "max_file_size_mb": 50,
                    "supported_extensions": [".xlsx", ".docx", ".pptx"],
                    "translation_service": "google"
                }
            }
        }