"""
DTOs para módulo de licencias
"""

from datetime import datetime
from enum import Enum
from typing import Optional

from pydantic import BaseModel, Field


class LicensePlanDTO(str, Enum):
    """Planes de licencia"""

    FREE = "free"
    BASIC = "basic"
    PROFESSIONAL = "professional"
    ENTERPRISE = "enterprise"


class LicenseStatusDTO(str, Enum):
    """Estados de licencia"""

    ACTIVE = "active"
    EXPIRED = "expired"
    SUSPENDED = "suspended"
    PENDING = "pending"
    CANCELLED = "cancelled"


class LicenseCreateDTO(BaseModel):
    """DTO para crear una nueva licencia"""

    tenant_id: int = Field(..., description="ID del tenant")
    plan: LicensePlanDTO = Field(..., description="Plan de licencia")
    max_users: int = Field(default=5, ge=1, description="Número máximo de usuarios")
    max_storage_gb: int = Field(
        default=10, ge=1, description="Almacenamiento máximo en GB"
    )
    max_monthly_operations: int = Field(
        default=1000, ge=1, description="Operaciones mensuales máximas"
    )

    feature_api_access: bool = Field(default=True)
    feature_advanced_reports: bool = Field(default=False)
    feature_integrations: bool = Field(default=False)
    feature_dedicated_support: bool = Field(default=False)

    starts_at: datetime = Field(..., description="Fecha de inicio de vigencia")
    expires_at: datetime = Field(..., description="Fecha de expiración")

    class Config:
        json_schema_extra = {
            "example": {
                "tenant_id": 1,
                "plan": "professional",
                "max_users": 20,
                "max_storage_gb": 100,
                "max_monthly_operations": 10000,
                "feature_api_access": True,
                "feature_advanced_reports": True,
                "feature_integrations": True,
                "feature_dedicated_support": False,
                "starts_at": "2025-01-01T00:00:00Z",
                "expires_at": "2025-12-31T23:59:59Z",
            }
        }


class LicenseUpdateDTO(BaseModel):
    """DTO para actualizar una licencia"""

    plan: Optional[LicensePlanDTO] = None
    status: Optional[LicenseStatusDTO] = None
    max_users: Optional[int] = Field(None, ge=1)
    max_storage_gb: Optional[int] = Field(None, ge=1)
    max_monthly_operations: Optional[int] = Field(None, ge=1)

    feature_api_access: Optional[bool] = None
    feature_advanced_reports: Optional[bool] = None
    feature_integrations: Optional[bool] = None
    feature_dedicated_support: Optional[bool] = None

    expires_at: Optional[datetime] = None


class LicenseResponseDTO(BaseModel):
    """DTO para respuesta de licencia"""

    id: int
    tenant_id: int
    plan: LicensePlanDTO
    status: LicenseStatusDTO

    max_users: int
    max_storage_gb: int
    max_monthly_operations: int

    feature_api_access: bool
    feature_advanced_reports: bool
    feature_integrations: bool
    feature_dedicated_support: bool

    starts_at: datetime
    expires_at: datetime
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class LicenseValidationResponseDTO(BaseModel):
    """DTO para respuesta de validación de licencia"""

    is_valid: bool
    status: LicenseStatusDTO
    plan: LicensePlanDTO
    expires_at: datetime
    reason: Optional[str] = None

    class Config:
        json_schema_extra = {
            "example": {
                "is_valid": True,
                "status": "active",
                "plan": "professional",
                "expires_at": "2025-12-31T23:59:59Z",
                "reason": None,
            }
        }


class LicenseUsageResponseDTO(BaseModel):
    """DTO para respuesta de uso de licencia"""

    tenant_id: int
    period_start: datetime
    period_end: datetime
    active_users: int
    storage_used_gb: int
    operations_count: int
    api_calls_count: int

    # Límites actuales
    max_users: int
    max_storage_gb: int
    max_monthly_operations: int

    # Porcentajes de uso
    users_usage_percent: float
    storage_usage_percent: float
    operations_usage_percent: float

    class Config:
        from_attributes = True
