"""Config response mappers.

Convert domain models (from config_service) to response models (for HTTP API).

This is the mapping layer at the router boundary, ensuring the service layer
remains independent of HTTP response shapes.
"""

from __future__ import annotations

from app.models.config import (
    BantimeEscalation,
    FilterConfig,
    FilterListResponse,
    GlobalConfigResponse,
    JailConfig,
    JailConfigListResponse,
    MapColorThresholdsResponse,
    RegexTestResponse,
    ServiceStatusResponse,
)
from app.models.config_domain import (
    DomainBantimeEscalation,
    DomainFilterConfig,
    DomainFilterList,
    DomainGlobalConfig,
    DomainJailConfig,
    DomainJailConfigList,
    DomainMapColorThresholds,
    DomainRegexTest,
    DomainServiceStatus,
)


def _map_domain_bantime_escalation(domain: DomainBantimeEscalation) -> BantimeEscalation:
    """Convert domain bantime escalation to response model."""
    return BantimeEscalation(
        increment=domain.increment,
        factor=domain.factor,
        formula=domain.formula,
        multipliers=domain.multipliers,
        max_time=domain.max_time,
        rnd_time=domain.rnd_time,
        overall_jails=domain.overall_jails,
    )


def map_domain_jail_config_to_response(domain: DomainJailConfig) -> JailConfig:
    """Convert domain jail config to response model."""
    return JailConfig(
        name=domain.name,
        ban_time=domain.ban_time,
        max_retry=domain.max_retry,
        find_time=domain.find_time,
        fail_regex=domain.fail_regex,
        ignore_regex=domain.ignore_regex,
        log_paths=domain.log_paths,
        date_pattern=domain.date_pattern,
        log_encoding=domain.log_encoding,
        backend=domain.backend,
        use_dns=domain.use_dns,
        prefregex=domain.prefregex,
        actions=domain.actions,
        bantime_escalation=(
            _map_domain_bantime_escalation(domain.bantime_escalation) if domain.bantime_escalation else None
        ),
    )


def map_domain_jail_config_list_to_response(
    domain_list: DomainJailConfigList,
) -> JailConfigListResponse:
    """Convert domain jail config list to response model."""
    return JailConfigListResponse(
        items=[map_domain_jail_config_to_response(c) for c in domain_list.items],
        total=domain_list.total,
    )


def map_domain_global_config_to_response(domain: DomainGlobalConfig) -> GlobalConfigResponse:
    """Convert domain global config to response model."""
    return GlobalConfigResponse(
        log_level=domain.log_level,
        log_target=domain.log_target,
        db_purge_age=domain.db_purge_age,
        db_max_matches=domain.db_max_matches,
    )


def map_domain_service_status_to_response(
    domain: DomainServiceStatus,
) -> ServiceStatusResponse:
    """Convert domain service status to response model."""
    return ServiceStatusResponse(
        online=domain.online,
        version=domain.version or "",
        jail_count=domain.jail_count,
        total_bans=domain.total_bans,
        total_failures=domain.total_failures,
        log_level=domain.log_level or "UNKNOWN",
        log_target=domain.log_target or "UNKNOWN",
    )


def map_domain_map_color_thresholds_to_response(
    domain: DomainMapColorThresholds,
) -> MapColorThresholdsResponse:
    """Convert domain map color thresholds to response model."""
    return MapColorThresholdsResponse(
        threshold_high=domain.threshold_high,
        threshold_medium=domain.threshold_medium,
        threshold_low=domain.threshold_low,
    )


def map_domain_regex_test_to_response(domain: DomainRegexTest) -> RegexTestResponse:
    """Convert domain regex test to response model."""
    return RegexTestResponse(
        matched=domain.matched,
        groups=domain.groups,
        error=domain.error,
    )


def map_domain_filter_config_to_response(domain: DomainFilterConfig) -> FilterConfig:
    """Convert domain filter config to response model."""
    return FilterConfig(
        name=domain.name,
        filename=domain.filename,
        before=domain.before,
        after=domain.after,
        variables=domain.variables or {},
        prefregex=domain.prefregex,
        failregex=domain.failregex or [],
        ignoreregex=domain.ignoreregex or [],
        maxlines=domain.maxlines,
        datepattern=domain.datepattern,
        journalmatch=domain.journalmatch,
        active=domain.active,
        used_by_jails=domain.used_by_jails or [],
        source_file=domain.source_file,
        has_local_override=domain.has_local_override,
    )


def map_domain_filter_list_to_response(domain_list: DomainFilterList) -> FilterListResponse:
    """Convert domain filter list to response model."""
    return FilterListResponse(
        filters=[map_domain_filter_config_to_response(f) for f in domain_list.items],
        total=domain_list.total,
    )
