from __future__ import annotations

import os
import shutil

from cybervisor.adapters.registry import get_adapter
from cybervisor.config import PipelineConfig


def run_preflight(config: PipelineConfig) -> list[str]:
    errors: list[str] = []
    adapter = get_adapter(config.agent_tool)
    requirements = adapter.preflight_requirements()

    for binary in requirements.required_binaries:
        if shutil.which(binary) is None:
            errors.append(
                (
                    f"Required tool '{binary}' was not found on PATH. "
                    f"Install it and verify with `{binary} --version`."
                )
            )

    for env_name in requirements.required_environment_variables:
        if env_name == "CYBERVISOR_LLM_API_KEY" and not (config.hook.api_key or os.getenv(env_name)):
            errors.append(
                (
                    "CYBERVISOR_LLM_API_KEY is required for non-mock execution. "
                    "Export it in your shell environment. "
                    "Alternatively, set it in a local .env file in your working directory."
                )
            )

    return errors
