from dataclasses import dataclass


@dataclass(frozen=True)
class HyperSearchRuntimeResources:
    cpu_resource: int
    cpu_usage: int
    optuna_n_jobs: int


_hyper_search_runtime_resources = HyperSearchRuntimeResources(
    cpu_resource=1,
    cpu_usage=1,
    optuna_n_jobs=1,
)


def calculate_hyper_search_runtime_resources(
    cpu_resource: int,
) -> HyperSearchRuntimeResources:
    normalized_cpu_resource = max(int(cpu_resource), 1)
    return HyperSearchRuntimeResources(
        cpu_resource=normalized_cpu_resource,
        cpu_usage=normalized_cpu_resource,
        optuna_n_jobs=max(normalized_cpu_resource - 1, 1),
    )


def configure_hyper_search_runtime_resources(
    cpu_resource: int,
) -> HyperSearchRuntimeResources:
    global _hyper_search_runtime_resources
    _hyper_search_runtime_resources = calculate_hyper_search_runtime_resources(
        cpu_resource
    )
    return _hyper_search_runtime_resources


def get_hyper_search_runtime_resources() -> HyperSearchRuntimeResources:
    return _hyper_search_runtime_resources
