from __future__ import annotations

from pathlib import Path

import pytest

from evals.swebench.io import write_instance_ids, write_predictions
from evals.swebench.smoke import prepare_scoring_smoke_subset
from evals.swebench.types import PredictionRecord


def _records(ids: list[str], model_tag: str) -> list[PredictionRecord]:
    return [
        PredictionRecord(
            instance_id=item,
            model_patch="diff --git a/a.py b/a.py\n",
            model_name_or_path=model_tag,
        )
        for item in ids
    ]


def test_prepare_scoring_smoke_subset_writes_paired_subset(tmp_path) -> None:
    source = tmp_path / "source-run"
    (source / "baseline").mkdir(parents=True, exist_ok=True)
    (source / "llmdebug").mkdir(parents=True, exist_ok=True)
    ids = ["inst-1", "inst-2", "inst-3"]
    write_predictions(source / "baseline" / "predictions.jsonl", _records(ids, "baseline-model"))
    write_predictions(source / "llmdebug" / "predictions.jsonl", _records(ids, "llmdebug-model"))
    write_instance_ids(source / "instance_ids.txt", ids)

    output = tmp_path / "smoke-run"
    summary = prepare_scoring_smoke_subset(
        source_run_dir=source,
        output_run_dir=output,
        max_instances=2,
        conditions="baseline,llmdebug",
    )

    assert summary.instance_count == 2
    assert (output / "baseline" / "predictions.jsonl").exists()
    assert (output / "llmdebug" / "predictions.jsonl").exists()
    assert (output / "COPY_INSTRUCTIONS.txt").exists()
    assert (output / "run_index.json").exists()
    assert (output / "run_metadata.json").exists()
    assert (output / "instance_ids.txt").read_text(encoding="utf-8").splitlines() == [
        "inst-1",
        "inst-2",
    ]


def test_prepare_scoring_smoke_subset_fails_when_output_not_empty(tmp_path) -> None:
    source = tmp_path / "source-run"
    (source / "baseline").mkdir(parents=True, exist_ok=True)
    (source / "llmdebug").mkdir(parents=True, exist_ok=True)
    ids = ["inst-1", "inst-2"]
    write_predictions(source / "baseline" / "predictions.jsonl", _records(ids, "baseline-model"))
    write_predictions(source / "llmdebug" / "predictions.jsonl", _records(ids, "llmdebug-model"))
    write_instance_ids(source / "instance_ids.txt", ids)

    output = tmp_path / "smoke-run"
    output.mkdir(parents=True, exist_ok=True)
    (output / "keep.txt").write_text("x", encoding="utf-8")

    with pytest.raises(RuntimeError, match="already exists and is not empty"):
        prepare_scoring_smoke_subset(
            source_run_dir=source,
            output_run_dir=output,
            max_instances=1,
            conditions="baseline,llmdebug",
        )
