from pathlib import Path
from typing import Any

from inspect_ai import eval

from bench_af._abstract.objects import Detector, Environment, ModelOrganism


def run_detector(
    *,
    detector: Detector,
    model_organism: ModelOrganism,
    environment: Environment,
    eval_config: dict[str, Any],
    results_dir: Path,
) -> tuple[float, str]:
    """
    Run a detector on a model organism and environment.
    """
    detector.check_model_and_environment_supported(model_organism, environment)

    detector.modify_environment(environment)

    eval_verification_logs = eval(
        tasks=environment.get_observed_task(),
        model=model_organism.model,
        solver=model_organism.solver,
        tags=["verify_environment"],
        log_dir=str(results_dir / "logs"),
        **(eval_config or {}),
    )
    
    # TODO: Verify that the model organism is not misaligned with the environment

    detector.modify_model_organism(model_organism)
    
    eval_logs = eval(
        tasks=environment.get_observed_task(),
        model=model_organism.model,
        solver=model_organism.solver,
        tags=["run_detector"],
        log_dir=str(results_dir / "logs"),
        **(eval_config or {}),
    )

    return detector.get_prediction(eval_logs)
