"""Validation helpers for user/setting - Auto-generated"""

from typing import Any, TypedDict, NotRequired, Literal

# Import common validators from central _helpers module
from hfortix_fortios._helpers import (
    validate_enable_disable,
    validate_integer_range,
    validate_string_length,
    validate_port_number,
    validate_ip_address,
    validate_ipv6_address,
    validate_mac_address,
)

# ============================================================================
# Required Fields Validation
# Auto-generated from schema
# ============================================================================

# ⚠️  IMPORTANT: FortiOS schemas have known issues with required field marking:

# Do NOT use this list for strict validation - test with the actual FortiOS API!

# Fields marked as required (after filtering false positives)
REQUIRED_FIELDS = [
]

# Fields with defaults (optional)
FIELDS_WITH_DEFAULTS = {
    "auth-type": "http https ftp telnet",
    "auth-cert": "",
    "auth-ca-cert": "",
    "auth-secure-http": "disable",
    "auth-http-basic": "disable",
    "auth-ssl-allow-renegotiation": "disable",
    "auth-src-mac": "enable",
    "auth-on-demand": "implicitly",
    "auth-timeout": 5,
    "auth-timeout-type": "idle-timeout",
    "auth-portal-timeout": 3,
    "radius-ses-timeout-act": "hard-timeout",
    "auth-blackout-time": 0,
    "auth-invalid-max": 5,
    "auth-lockout-threshold": 3,
    "auth-lockout-duration": 0,
    "per-policy-disclaimer": "disable",
    "auth-ssl-min-proto-version": "default",
    "auth-ssl-max-proto-version": "",
    "auth-ssl-sigalgs": "all",
    "default-user-password-policy": "",
    "cors": "disable",
}

# ============================================================================
# Deprecated Fields
# Auto-generated from schema - warns users about deprecated fields
# ============================================================================

# Deprecated fields with migration guidance
DEPRECATED_FIELDS = {
}

# ============================================================================
# Field Metadata (Type Information & Descriptions)
# Auto-generated from schema - use for IDE autocomplete and documentation
# ============================================================================

# Field types mapping
FIELD_TYPES = {
    "auth-type": "option",  # Supported firewall policy authentication protocols/methods.
    "auth-cert": "string",  # HTTPS server certificate for policy authentication.
    "auth-ca-cert": "string",  # HTTPS CA certificate for policy authentication.
    "auth-secure-http": "option",  # Enable/disable redirecting HTTP user authentication to more 
    "auth-http-basic": "option",  # Enable/disable use of HTTP basic authentication for identity
    "auth-ssl-allow-renegotiation": "option",  # Allow/forbid SSL re-negotiation for HTTPS authentication.
    "auth-src-mac": "option",  # Enable/disable source MAC for user identity.
    "auth-on-demand": "option",  # Always/implicitly trigger firewall authentication on demand.
    "auth-timeout": "integer",  # Time in minutes before the firewall user authentication time
    "auth-timeout-type": "option",  # Control if authenticated users have to login again after a h
    "auth-portal-timeout": "integer",  # Time in minutes before captive portal user have to re-authen
    "radius-ses-timeout-act": "option",  # Set the RADIUS session timeout to a hard timeout or to ignor
    "auth-blackout-time": "integer",  # Time in seconds an IP address is denied access after failing
    "auth-invalid-max": "integer",  # Maximum number of failed authentication attempts before the 
    "auth-lockout-threshold": "integer",  # Maximum number of failed login attempts before login lockout
    "auth-lockout-duration": "integer",  # Lockout period in seconds after too many login failures.
    "per-policy-disclaimer": "option",  # Enable/disable per policy disclaimer.
    "auth-ports": "string",  # Set up non-standard ports for authentication with HTTP, HTTP
    "auth-ssl-min-proto-version": "option",  # Minimum supported protocol version for SSL/TLS connections (
    "auth-ssl-max-proto-version": "option",  # Maximum supported protocol version for SSL/TLS connections (
    "auth-ssl-sigalgs": "option",  # Set signature algorithms related to HTTPS authentication (af
    "default-user-password-policy": "string",  # Default password policy to apply to all local users unless o
    "cors": "option",  # Enable/disable allowed origins white list for CORS.
    "cors-allowed-origins": "string",  # Allowed origins white list for CORS.
}

# Field descriptions (help text from FortiOS API)
FIELD_DESCRIPTIONS = {
    "auth-type": "Supported firewall policy authentication protocols/methods.",
    "auth-cert": "HTTPS server certificate for policy authentication.",
    "auth-ca-cert": "HTTPS CA certificate for policy authentication.",
    "auth-secure-http": "Enable/disable redirecting HTTP user authentication to more secure HTTPS.",
    "auth-http-basic": "Enable/disable use of HTTP basic authentication for identity-based firewall policies.",
    "auth-ssl-allow-renegotiation": "Allow/forbid SSL re-negotiation for HTTPS authentication.",
    "auth-src-mac": "Enable/disable source MAC for user identity.",
    "auth-on-demand": "Always/implicitly trigger firewall authentication on demand.",
    "auth-timeout": "Time in minutes before the firewall user authentication timeout requires the user to re-authenticate.",
    "auth-timeout-type": "Control if authenticated users have to login again after a hard timeout, after an idle timeout, or after a session timeout.",
    "auth-portal-timeout": "Time in minutes before captive portal user have to re-authenticate (1 - 30 min, default 3 min).",
    "radius-ses-timeout-act": "Set the RADIUS session timeout to a hard timeout or to ignore RADIUS server session timeouts.",
    "auth-blackout-time": "Time in seconds an IP address is denied access after failing to authenticate five times within one minute.",
    "auth-invalid-max": "Maximum number of failed authentication attempts before the user is blocked.",
    "auth-lockout-threshold": "Maximum number of failed login attempts before login lockout is triggered.",
    "auth-lockout-duration": "Lockout period in seconds after too many login failures.",
    "per-policy-disclaimer": "Enable/disable per policy disclaimer.",
    "auth-ports": "Set up non-standard ports for authentication with HTTP, HTTPS, FTP, and TELNET.",
    "auth-ssl-min-proto-version": "Minimum supported protocol version for SSL/TLS connections (default is to follow system global setting).",
    "auth-ssl-max-proto-version": "Maximum supported protocol version for SSL/TLS connections (default is no limit).",
    "auth-ssl-sigalgs": "Set signature algorithms related to HTTPS authentication (affects TLS version <= 1.2 only, default is to enable all).",
    "default-user-password-policy": "Default password policy to apply to all local users unless otherwise specified, as defined in config user password-policy.",
    "cors": "Enable/disable allowed origins white list for CORS.",
    "cors-allowed-origins": "Allowed origins white list for CORS.",
}

# Field constraints (string lengths, integer ranges)
FIELD_CONSTRAINTS = {
    "auth-cert": {"type": "string", "max_length": 35},
    "auth-ca-cert": {"type": "string", "max_length": 35},
    "auth-timeout": {"type": "integer", "min": 1, "max": 1440},
    "auth-portal-timeout": {"type": "integer", "min": 1, "max": 30},
    "auth-blackout-time": {"type": "integer", "min": 0, "max": 3600},
    "auth-invalid-max": {"type": "integer", "min": 1, "max": 100},
    "auth-lockout-threshold": {"type": "integer", "min": 1, "max": 10},
    "auth-lockout-duration": {"type": "integer", "min": 0, "max": 4294967295},
    "default-user-password-policy": {"type": "string", "max_length": 35},
}

# Nested schemas (for table/list fields with children)
NESTED_SCHEMAS = {
    "auth-ports": {
        "id": {
            "type": "integer",
            "help": "ID.",
            "required": True,
            "default": 0,
            "min_value": 0,
            "max_value": 4294967295,
        },
        "type": {
            "type": "option",
            "help": "Service type.",
            "default": "http",
            "options": [{"help": "HTTP service.", "label": "Http", "name": "http"}, {"help": "HTTPS service.", "label": "Https", "name": "https"}, {"help": "FTP service.", "label": "Ftp", "name": "ftp"}, {"help": "TELNET service.", "label": "Telnet", "name": "telnet"}],
        },
        "port": {
            "type": "integer",
            "help": "Non-standard port for firewall user authentication.",
            "default": 1024,
            "min_value": 1,
            "max_value": 65535,
        },
    },
    "cors-allowed-origins": {
        "name": {
            "type": "string",
            "help": "Allowed origin for CORS.",
            "default": "",
            "max_length": 79,
        },
    },
}


# Valid enum values from API documentation
VALID_BODY_AUTH_TYPE = [
    "http",  # Allow HTTP authentication.
    "https",  # Allow HTTPS authentication.
    "ftp",  # Allow FTP authentication.
    "telnet",  # Allow TELNET authentication.
]
VALID_BODY_AUTH_SECURE_HTTP = [
    "enable",  # Enable setting.
    "disable",  # Disable setting.
]
VALID_BODY_AUTH_HTTP_BASIC = [
    "enable",  # Enable setting.
    "disable",  # Disable setting.
]
VALID_BODY_AUTH_SSL_ALLOW_RENEGOTIATION = [
    "enable",  # Allow SSL re-negotiation.
    "disable",  # Forbid SSL re-negotiation.
]
VALID_BODY_AUTH_SRC_MAC = [
    "enable",  # Enable source MAC for user identity.
    "disable",  # Disable source MAC for user identity.
]
VALID_BODY_AUTH_ON_DEMAND = [
    "always",  # Always trigger firewall authentication on demand.
    "implicitly",  # Implicitly trigger firewall authentication on demand.
]
VALID_BODY_AUTH_TIMEOUT_TYPE = [
    "idle-timeout",  # Idle timeout.
    "hard-timeout",  # Hard timeout.
    "new-session",  # New session timeout.
]
VALID_BODY_RADIUS_SES_TIMEOUT_ACT = [
    "hard-timeout",  # Use session timeout from RADIUS as hard-timeout.
    "ignore-timeout",  # Ignore session timeout from RADIUS.
]
VALID_BODY_PER_POLICY_DISCLAIMER = [
    "enable",  # Enable per policy disclaimer.
    "disable",  # Disable per policy disclaimer.
]
VALID_BODY_AUTH_SSL_MIN_PROTO_VERSION = [
    "default",  # Follow system global setting.
    "SSLv3",  # SSLv3.
    "TLSv1",  # TLSv1.
    "TLSv1-1",  # TLSv1.1.
    "TLSv1-2",  # TLSv1.2.
    "TLSv1-3",  # TLSv1.3.
]
VALID_BODY_AUTH_SSL_MAX_PROTO_VERSION = [
    "sslv3",  # SSLv3.
    "tlsv1",  # TLSv1.
    "tlsv1-1",  # TLSv1.1.
    "tlsv1-2",  # TLSv1.2.
    "tlsv1-3",  # TLSv1.3.
]
VALID_BODY_AUTH_SSL_SIGALGS = [
    "no-rsa-pss",  # Disable RSA-PSS signature algorithms for HTTPS authentication.
    "all",  # Enable all supported signature algorithms for HTTPS authentication.
]
VALID_BODY_CORS = [
    "disable",  # Disable allowed origins white list for CORS.
    "enable",  # Enable allowed origins white list for CORS.
]
VALID_QUERY_ACTION = ["default", "schema"]

# ============================================================================
# GET Validation
# ============================================================================


def validate_user_setting_get(
    attr: str | None = None,
    filters: dict[str, Any] | None = None,
    **params: Any,
) -> tuple[bool, str | None]:
    """Validate GET request parameters for user/setting."""
    # Validate query parameters if present
    if "action" in params:
        value = params.get("action")
        if value and value not in VALID_QUERY_ACTION:
            return (
                False,
                f"Invalid query parameter 'action'='{value}'. Must be one of: {', '.join(VALID_QUERY_ACTION)}",
            )

    return (True, None)


# ============================================================================
# POST Validation
# ============================================================================


def validate_required_fields(payload: dict) -> tuple[bool, str | None]:
    """Validate required fields for user/setting."""
    # Check always-required fields
    missing_fields = []
    for field in REQUIRED_FIELDS:
        if field not in payload:
            missing_fields.append(field)
    
    if missing_fields:
        # Build enhanced error message
        error_parts = [f"Missing required field(s): {', '.join(missing_fields)}"]
        
        # Add descriptions for first few missing fields
        for field in missing_fields[:3]:
            desc = FIELD_DESCRIPTIONS.get(field)
            if desc:
                error_parts.append(f"  • {field}: {desc}")
        
        if len(missing_fields) > 3:
            error_parts.append(f"  ... and {len(missing_fields) - 3} more")
        
        return (False, "\n".join(error_parts))

    return (True, None)


def validate_user_setting_post(
    payload: dict,
    **params: Any,
) -> tuple[bool, str | None]:
    """Validate POST request to create new user/setting object."""
    # Step 1: Validate required fields
    is_valid, error = validate_required_fields(payload)
    if not is_valid:
        return (False, error)

    # Step 2: Validate enum values
    if "auth-type" in payload:
        value = payload["auth-type"]
        if value not in VALID_BODY_AUTH_TYPE:
            desc = FIELD_DESCRIPTIONS.get("auth-type", "")
            error_msg = f"Invalid value for 'auth-type': '{value}'"
            if desc:
                error_msg += f"\n  → Description: {desc}"
            error_msg += f"\n  → Valid options: {', '.join(repr(v) for v in VALID_BODY_AUTH_TYPE)}"
            error_msg += f"\n  → Example: auth-type='{{ VALID_BODY_AUTH_TYPE[0] }}'"
            return (False, error_msg)
    if "auth-secure-http" in payload:
        value = payload["auth-secure-http"]
        if value not in VALID_BODY_AUTH_SECURE_HTTP:
            desc = FIELD_DESCRIPTIONS.get("auth-secure-http", "")
            error_msg = f"Invalid value for 'auth-secure-http': '{value}'"
            if desc:
                error_msg += f"\n  → Description: {desc}"
            error_msg += f"\n  → Valid options: {', '.join(repr(v) for v in VALID_BODY_AUTH_SECURE_HTTP)}"
            error_msg += f"\n  → Example: auth-secure-http='{{ VALID_BODY_AUTH_SECURE_HTTP[0] }}'"
            return (False, error_msg)
    if "auth-http-basic" in payload:
        value = payload["auth-http-basic"]
        if value not in VALID_BODY_AUTH_HTTP_BASIC:
            desc = FIELD_DESCRIPTIONS.get("auth-http-basic", "")
            error_msg = f"Invalid value for 'auth-http-basic': '{value}'"
            if desc:
                error_msg += f"\n  → Description: {desc}"
            error_msg += f"\n  → Valid options: {', '.join(repr(v) for v in VALID_BODY_AUTH_HTTP_BASIC)}"
            error_msg += f"\n  → Example: auth-http-basic='{{ VALID_BODY_AUTH_HTTP_BASIC[0] }}'"
            return (False, error_msg)
    if "auth-ssl-allow-renegotiation" in payload:
        value = payload["auth-ssl-allow-renegotiation"]
        if value not in VALID_BODY_AUTH_SSL_ALLOW_RENEGOTIATION:
            desc = FIELD_DESCRIPTIONS.get("auth-ssl-allow-renegotiation", "")
            error_msg = f"Invalid value for 'auth-ssl-allow-renegotiation': '{value}'"
            if desc:
                error_msg += f"\n  → Description: {desc}"
            error_msg += f"\n  → Valid options: {', '.join(repr(v) for v in VALID_BODY_AUTH_SSL_ALLOW_RENEGOTIATION)}"
            error_msg += f"\n  → Example: auth-ssl-allow-renegotiation='{{ VALID_BODY_AUTH_SSL_ALLOW_RENEGOTIATION[0] }}'"
            return (False, error_msg)
    if "auth-src-mac" in payload:
        value = payload["auth-src-mac"]
        if value not in VALID_BODY_AUTH_SRC_MAC:
            desc = FIELD_DESCRIPTIONS.get("auth-src-mac", "")
            error_msg = f"Invalid value for 'auth-src-mac': '{value}'"
            if desc:
                error_msg += f"\n  → Description: {desc}"
            error_msg += f"\n  → Valid options: {', '.join(repr(v) for v in VALID_BODY_AUTH_SRC_MAC)}"
            error_msg += f"\n  → Example: auth-src-mac='{{ VALID_BODY_AUTH_SRC_MAC[0] }}'"
            return (False, error_msg)
    if "auth-on-demand" in payload:
        value = payload["auth-on-demand"]
        if value not in VALID_BODY_AUTH_ON_DEMAND:
            desc = FIELD_DESCRIPTIONS.get("auth-on-demand", "")
            error_msg = f"Invalid value for 'auth-on-demand': '{value}'"
            if desc:
                error_msg += f"\n  → Description: {desc}"
            error_msg += f"\n  → Valid options: {', '.join(repr(v) for v in VALID_BODY_AUTH_ON_DEMAND)}"
            error_msg += f"\n  → Example: auth-on-demand='{{ VALID_BODY_AUTH_ON_DEMAND[0] }}'"
            return (False, error_msg)
    if "auth-timeout-type" in payload:
        value = payload["auth-timeout-type"]
        if value not in VALID_BODY_AUTH_TIMEOUT_TYPE:
            desc = FIELD_DESCRIPTIONS.get("auth-timeout-type", "")
            error_msg = f"Invalid value for 'auth-timeout-type': '{value}'"
            if desc:
                error_msg += f"\n  → Description: {desc}"
            error_msg += f"\n  → Valid options: {', '.join(repr(v) for v in VALID_BODY_AUTH_TIMEOUT_TYPE)}"
            error_msg += f"\n  → Example: auth-timeout-type='{{ VALID_BODY_AUTH_TIMEOUT_TYPE[0] }}'"
            return (False, error_msg)
    if "radius-ses-timeout-act" in payload:
        value = payload["radius-ses-timeout-act"]
        if value not in VALID_BODY_RADIUS_SES_TIMEOUT_ACT:
            desc = FIELD_DESCRIPTIONS.get("radius-ses-timeout-act", "")
            error_msg = f"Invalid value for 'radius-ses-timeout-act': '{value}'"
            if desc:
                error_msg += f"\n  → Description: {desc}"
            error_msg += f"\n  → Valid options: {', '.join(repr(v) for v in VALID_BODY_RADIUS_SES_TIMEOUT_ACT)}"
            error_msg += f"\n  → Example: radius-ses-timeout-act='{{ VALID_BODY_RADIUS_SES_TIMEOUT_ACT[0] }}'"
            return (False, error_msg)
    if "per-policy-disclaimer" in payload:
        value = payload["per-policy-disclaimer"]
        if value not in VALID_BODY_PER_POLICY_DISCLAIMER:
            desc = FIELD_DESCRIPTIONS.get("per-policy-disclaimer", "")
            error_msg = f"Invalid value for 'per-policy-disclaimer': '{value}'"
            if desc:
                error_msg += f"\n  → Description: {desc}"
            error_msg += f"\n  → Valid options: {', '.join(repr(v) for v in VALID_BODY_PER_POLICY_DISCLAIMER)}"
            error_msg += f"\n  → Example: per-policy-disclaimer='{{ VALID_BODY_PER_POLICY_DISCLAIMER[0] }}'"
            return (False, error_msg)
    if "auth-ssl-min-proto-version" in payload:
        value = payload["auth-ssl-min-proto-version"]
        if value not in VALID_BODY_AUTH_SSL_MIN_PROTO_VERSION:
            desc = FIELD_DESCRIPTIONS.get("auth-ssl-min-proto-version", "")
            error_msg = f"Invalid value for 'auth-ssl-min-proto-version': '{value}'"
            if desc:
                error_msg += f"\n  → Description: {desc}"
            error_msg += f"\n  → Valid options: {', '.join(repr(v) for v in VALID_BODY_AUTH_SSL_MIN_PROTO_VERSION)}"
            error_msg += f"\n  → Example: auth-ssl-min-proto-version='{{ VALID_BODY_AUTH_SSL_MIN_PROTO_VERSION[0] }}'"
            return (False, error_msg)
    if "auth-ssl-max-proto-version" in payload:
        value = payload["auth-ssl-max-proto-version"]
        if value not in VALID_BODY_AUTH_SSL_MAX_PROTO_VERSION:
            desc = FIELD_DESCRIPTIONS.get("auth-ssl-max-proto-version", "")
            error_msg = f"Invalid value for 'auth-ssl-max-proto-version': '{value}'"
            if desc:
                error_msg += f"\n  → Description: {desc}"
            error_msg += f"\n  → Valid options: {', '.join(repr(v) for v in VALID_BODY_AUTH_SSL_MAX_PROTO_VERSION)}"
            error_msg += f"\n  → Example: auth-ssl-max-proto-version='{{ VALID_BODY_AUTH_SSL_MAX_PROTO_VERSION[0] }}'"
            return (False, error_msg)
    if "auth-ssl-sigalgs" in payload:
        value = payload["auth-ssl-sigalgs"]
        if value not in VALID_BODY_AUTH_SSL_SIGALGS:
            desc = FIELD_DESCRIPTIONS.get("auth-ssl-sigalgs", "")
            error_msg = f"Invalid value for 'auth-ssl-sigalgs': '{value}'"
            if desc:
                error_msg += f"\n  → Description: {desc}"
            error_msg += f"\n  → Valid options: {', '.join(repr(v) for v in VALID_BODY_AUTH_SSL_SIGALGS)}"
            error_msg += f"\n  → Example: auth-ssl-sigalgs='{{ VALID_BODY_AUTH_SSL_SIGALGS[0] }}'"
            return (False, error_msg)
    if "cors" in payload:
        value = payload["cors"]
        if value not in VALID_BODY_CORS:
            desc = FIELD_DESCRIPTIONS.get("cors", "")
            error_msg = f"Invalid value for 'cors': '{value}'"
            if desc:
                error_msg += f"\n  → Description: {desc}"
            error_msg += f"\n  → Valid options: {', '.join(repr(v) for v in VALID_BODY_CORS)}"
            error_msg += f"\n  → Example: cors='{{ VALID_BODY_CORS[0] }}'"
            return (False, error_msg)

    return (True, None)


# ============================================================================
# PUT Validation
# ============================================================================


def validate_user_setting_put(
    payload: dict,
    **params: Any,
) -> tuple[bool, str | None]:
    """Validate PUT request to update user/setting."""
    # Step 1: Validate enum values
    if "auth-type" in payload:
        value = payload["auth-type"]
        if value not in VALID_BODY_AUTH_TYPE:
            return (
                False,
                f"Invalid value for 'auth-type'='{value}'. Must be one of: {', '.join(VALID_BODY_AUTH_TYPE)}",
            )
    if "auth-secure-http" in payload:
        value = payload["auth-secure-http"]
        if value not in VALID_BODY_AUTH_SECURE_HTTP:
            return (
                False,
                f"Invalid value for 'auth-secure-http'='{value}'. Must be one of: {', '.join(VALID_BODY_AUTH_SECURE_HTTP)}",
            )
    if "auth-http-basic" in payload:
        value = payload["auth-http-basic"]
        if value not in VALID_BODY_AUTH_HTTP_BASIC:
            return (
                False,
                f"Invalid value for 'auth-http-basic'='{value}'. Must be one of: {', '.join(VALID_BODY_AUTH_HTTP_BASIC)}",
            )
    if "auth-ssl-allow-renegotiation" in payload:
        value = payload["auth-ssl-allow-renegotiation"]
        if value not in VALID_BODY_AUTH_SSL_ALLOW_RENEGOTIATION:
            return (
                False,
                f"Invalid value for 'auth-ssl-allow-renegotiation'='{value}'. Must be one of: {', '.join(VALID_BODY_AUTH_SSL_ALLOW_RENEGOTIATION)}",
            )
    if "auth-src-mac" in payload:
        value = payload["auth-src-mac"]
        if value not in VALID_BODY_AUTH_SRC_MAC:
            return (
                False,
                f"Invalid value for 'auth-src-mac'='{value}'. Must be one of: {', '.join(VALID_BODY_AUTH_SRC_MAC)}",
            )
    if "auth-on-demand" in payload:
        value = payload["auth-on-demand"]
        if value not in VALID_BODY_AUTH_ON_DEMAND:
            return (
                False,
                f"Invalid value for 'auth-on-demand'='{value}'. Must be one of: {', '.join(VALID_BODY_AUTH_ON_DEMAND)}",
            )
    if "auth-timeout-type" in payload:
        value = payload["auth-timeout-type"]
        if value not in VALID_BODY_AUTH_TIMEOUT_TYPE:
            return (
                False,
                f"Invalid value for 'auth-timeout-type'='{value}'. Must be one of: {', '.join(VALID_BODY_AUTH_TIMEOUT_TYPE)}",
            )
    if "radius-ses-timeout-act" in payload:
        value = payload["radius-ses-timeout-act"]
        if value not in VALID_BODY_RADIUS_SES_TIMEOUT_ACT:
            return (
                False,
                f"Invalid value for 'radius-ses-timeout-act'='{value}'. Must be one of: {', '.join(VALID_BODY_RADIUS_SES_TIMEOUT_ACT)}",
            )
    if "per-policy-disclaimer" in payload:
        value = payload["per-policy-disclaimer"]
        if value not in VALID_BODY_PER_POLICY_DISCLAIMER:
            return (
                False,
                f"Invalid value for 'per-policy-disclaimer'='{value}'. Must be one of: {', '.join(VALID_BODY_PER_POLICY_DISCLAIMER)}",
            )
    if "auth-ssl-min-proto-version" in payload:
        value = payload["auth-ssl-min-proto-version"]
        if value not in VALID_BODY_AUTH_SSL_MIN_PROTO_VERSION:
            return (
                False,
                f"Invalid value for 'auth-ssl-min-proto-version'='{value}'. Must be one of: {', '.join(VALID_BODY_AUTH_SSL_MIN_PROTO_VERSION)}",
            )
    if "auth-ssl-max-proto-version" in payload:
        value = payload["auth-ssl-max-proto-version"]
        if value not in VALID_BODY_AUTH_SSL_MAX_PROTO_VERSION:
            return (
                False,
                f"Invalid value for 'auth-ssl-max-proto-version'='{value}'. Must be one of: {', '.join(VALID_BODY_AUTH_SSL_MAX_PROTO_VERSION)}",
            )
    if "auth-ssl-sigalgs" in payload:
        value = payload["auth-ssl-sigalgs"]
        if value not in VALID_BODY_AUTH_SSL_SIGALGS:
            return (
                False,
                f"Invalid value for 'auth-ssl-sigalgs'='{value}'. Must be one of: {', '.join(VALID_BODY_AUTH_SSL_SIGALGS)}",
            )
    if "cors" in payload:
        value = payload["cors"]
        if value not in VALID_BODY_CORS:
            return (
                False,
                f"Invalid value for 'cors'='{value}'. Must be one of: {', '.join(VALID_BODY_CORS)}",
            )

    return (True, None)


# ============================================================================
# Metadata Access Functions
# Imported from central module to avoid duplication across 1,062 files
# ============================================================================

from hfortix_fortios._helpers.metadata import (
    get_field_description as _get_field_description,
    get_field_type as _get_field_type,
    get_field_constraints as _get_field_constraints,
    get_field_default as _get_field_default,
    get_field_options as _get_field_options,
    get_nested_schema as _get_nested_schema,
    get_all_fields as _get_all_fields,
    get_field_metadata as _get_field_metadata,
    validate_field_value as _validate_field_value,
)

# Wrapper functions that bind module-specific data to central functions
def get_field_description(field_name: str) -> str | None:
    """Get description/help text for a field."""
    return _get_field_description(FIELD_DESCRIPTIONS, field_name)

def get_field_type(field_name: str) -> str | None:
    """Get the type of a field."""
    return _get_field_type(FIELD_TYPES, field_name)

def get_field_constraints(field_name: str) -> dict[str, Any] | None:
    """Get constraints for a field (min/max values, string length)."""
    return _get_field_constraints(FIELD_CONSTRAINTS, field_name)

def get_field_default(field_name: str) -> Any | None:
    """Get default value for a field."""
    return _get_field_default(FIELDS_WITH_DEFAULTS, field_name)

def get_field_options(field_name: str) -> list[str] | None:
    """Get valid enum options for a field."""
    return _get_field_options(globals(), field_name)

def get_nested_schema(field_name: str) -> dict[str, Any] | None:
    """Get schema for nested table/list fields."""
    return _get_nested_schema(NESTED_SCHEMAS, field_name)

def get_all_fields() -> list[str]:
    """Get list of all field names."""
    return _get_all_fields(FIELD_TYPES)

def get_field_metadata(field_name: str) -> dict[str, Any] | None:
    """Get complete metadata for a field (type, description, constraints, defaults, options)."""
    return _get_field_metadata(
        FIELD_TYPES, FIELD_DESCRIPTIONS, FIELD_CONSTRAINTS,
        FIELDS_WITH_DEFAULTS, REQUIRED_FIELDS, NESTED_SCHEMAS,
        globals(), field_name
    )

def validate_field_value(field_name: str, value: Any) -> tuple[bool, str | None]:
    """Validate a single field value against its constraints."""
    return _validate_field_value(
        FIELD_TYPES, FIELD_DESCRIPTIONS, FIELD_CONSTRAINTS,
        globals(), field_name, value
    )


# ============================================================================
# Schema Information
# Metadata about this endpoint schema
# ============================================================================

SCHEMA_INFO = {
    "endpoint": "user/setting",
    "category": "cmdb",
    "api_path": "user/setting",
    "help": "Configure user authentication setting.",
    "total_fields": 24,
    "required_fields_count": 0,
    "fields_with_defaults_count": 22,
}


def get_schema_info() -> dict[str, Any]:
    """Get information about this endpoint schema."""
    return SCHEMA_INFO.copy()
