from __future__ import annotations

from pathlib import Path

from core.config.settings import RuntimeSettings


def build_file_handler_config(
    runtime: RuntimeSettings,
    file_path: Path,
    level: str,
    formatter: str,
    filters: list[str] | None = None,
) -> dict[str, object]:
    filter_list = list(filters or [])
    base_config: dict[str, object] = {
        "level": level,
        "formatter": formatter,
        "filename": str(file_path),
        "encoding": "utf-8",
    }

    if filter_list:
        base_config = {**base_config, "filters": filter_list}

    if runtime.log_rotation == "time":
        return {
            **base_config,
            "class": "logging.handlers.TimedRotatingFileHandler",
            "when": runtime.log_rotation_when,
            "interval": runtime.log_rotation_interval,
            "backupCount": runtime.log_rotation_backup_count,
        }

    if runtime.log_rotation == "size":
        return {
            **base_config,
            "class": "logging.handlers.RotatingFileHandler",
            "maxBytes": runtime.log_rotation_max_bytes,
            "backupCount": runtime.log_rotation_backup_count,
        }

    return {
        **base_config,
        "class": "logging.FileHandler",
    }
