"""Optional Inspect tasks sharing the exact development data and scorers.

Importing this explicit integration module requires the ``inspect`` extra. The core
package and fixture replay never import Inspect. Inspect CLI/runtime owns model
execution, overrides, logs, concurrency, and errors for these task adapters.
"""

from __future__ import annotations

import json

try:
    from inspect_ai import Task, task
    from inspect_ai.dataset import Sample
    from inspect_ai.model import GenerateConfig
    from inspect_ai.scorer import CORRECT, INCORRECT, Score, accuracy, scorer
    from inspect_ai.solver import generate
except ImportError as error:
    raise ImportError("Inspect integration requires: pip install -e '.[inspect]'") from error

from hessian_recipes.evaluation import load_spec, score_sample


@scorer(metrics=[accuracy()])
def deterministic_development_scorer():
    async def score(state, target):
        sample = json.loads(target.text)
        correct, details = score_sample(sample, state.output.completion)
        return Score(
            value=CORRECT if correct else INCORRECT,
            answer=state.output.completion,
            explanation=json.dumps(details, sort_keys=True),
        )

    return score


@task
def development_benchmark(spec_path: str):
    """Inspect task; the spec freezes fixture/scorer identity, not Inspect overrides."""
    spec, samples, _ = load_spec(spec_path)
    if spec["model"].get("chat_template_kwargs"):
        raise ValueError(
            "Inspect adapter does not freeze local chat_template_kwargs; use the Transformers adapter for explicit thinking mode"
        )
    if "min_p" in spec["sampling"]:
        raise ValueError(
            "this Inspect version does not support frozen min_p; use the local Transformers adapter"
        )
    return Task(
        dataset=[
            Sample(
                id=row["id"],
                input=row["prompt"],
                target=json.dumps(row),
                metadata={"category": row["category"]},
            )
            for row in samples
        ],
        solver=generate(),
        scorer=deterministic_development_scorer(),
        epochs=spec["trials"],
        config=GenerateConfig(
            temperature=spec["sampling"]["temperature"],
            max_tokens=spec["sampling"]["max_new_tokens"],
            seed=spec["sampling"]["seed"],
            **{key: spec["sampling"][key] for key in ("top_p", "top_k") if key in spec["sampling"]},
        ),
        fail_on_error=True,
        version=spec["benchmark"]["version"],
        metadata={
            "benchmark": spec["benchmark"],
            "release_eligible": False,
            "notice": "Development fixtures. Inspect model/config overrides must be audited from the effective Inspect log; this task does not freeze its runtime.",
        },
    )
