"""
Schemi Pydantic per Audit Log.
"""

import uuid
from datetime import datetime
from typing import Any, Optional

from pydantic import BaseModel, field_validator

from app.core.pagination import PaginatedResponse


class AuditLogResponse(BaseModel):
    """Risposta singolo evento audit."""

    id: int
    tenant_id: Optional[uuid.UUID] = None
    user_id: Optional[uuid.UUID] = None
    # Dati utente denormalizzati (arricchiti dalla JOIN nel service)
    user_email: Optional[str] = None
    user_full_name: Optional[str] = None
    action: str
    resource_type: Optional[str] = None
    resource_id: Optional[uuid.UUID] = None
    ip_address: Optional[str] = None
    user_agent: Optional[str] = None
    payload: Optional[dict] = None
    outcome: str
    occurred_at: datetime

    model_config = {"from_attributes": True}

    @field_validator("ip_address", mode="before")
    @classmethod
    def coerce_ip_address(cls, v: Any) -> Optional[str]:
        """Converte IPv4Address/IPv6Address (tipo PostgreSQL INET) in stringa."""
        if v is None:
            return None
        return str(v)


# Lista paginata
AuditLogListResponse = PaginatedResponse[AuditLogResponse]
