"""Server status and health-check Pydantic models.

Used by the dashboard router, health service, and server settings router.

All models inherit from :class:`~app.models.response.BanGuiBaseModel` which
enforces the project-wide **snake_case** API field naming policy: field names
are identical in Python, JSON wire format, and the corresponding TypeScript
interfaces.
"""

from pydantic import Field

from app.models.response import BanGuiBaseModel


class ServerStatus(BanGuiBaseModel):
    """Cached fail2ban server health snapshot."""

    online: bool = Field(..., description="Whether fail2ban is reachable via its socket.")
    version: str | None = Field(default=None, description="fail2ban version string.")
    active_jails: int = Field(default=0, ge=0, description="Number of currently active jails.")
    total_bans: int = Field(default=0, ge=0, description="Aggregated current ban count across all jails.")
    total_failures: int = Field(default=0, ge=0, description="Aggregated current failure count across all jails.")


class ServerStatusResponse(BanGuiBaseModel):
    """Response for ``GET /api/dashboard/status``."""

    status: ServerStatus


class ServerSettings(BanGuiBaseModel):
    """Domain model for fail2ban server-level settings."""

    log_level: str = Field(..., description="fail2ban daemon log level.")
    log_target: str = Field(..., description="Log destination: STDOUT, STDERR, SYSLOG, or a file path.")
    syslog_socket: str | None = Field(default=None)
    db_path: str = Field(..., description="Path to the fail2ban ban history database.")
    db_purge_age: int = Field(..., description="Seconds before old records are purged.")
    db_max_matches: int = Field(..., description="Maximum stored matches per ban record.")


class ServerSettingsUpdate(BanGuiBaseModel):
    """Payload for ``PUT /api/server/settings``."""

    log_level: str | None = Field(default=None)
    log_target: str | None = Field(default=None)
    db_purge_age: int | None = Field(default=None, ge=0)
    db_max_matches: int | None = Field(default=None, ge=0)


class ServerSettingsResponse(BanGuiBaseModel):
    """Response for ``GET /api/server/settings``."""

    settings: ServerSettings
    warnings: dict[str, bool] = Field(
        default_factory=dict,
        description="Warnings highlighting potentially unsafe settings.",
    )
