# SPDX-License-Identifier: AGPL-3.0-or-later
"""R13 review F2: the headline oracle-grade outcome must not collapse a
non-pass/fail category to FAIL."""
from __future__ import annotations

from helpers import build_run_config

from orchestrator import observability
from orchestrator.controller import Controller
from orchestrator.executor import ScriptedExecutor
from orchestrator.store import EventStore

RUN_ID = "RUN-R13-OUTCOME"


def _admit_hidden(run_id, controller, node_id, *, checks, effective, initial, classification):
    controller._emit(
        run_id,
        "EVIDENCE_ADMITTED",
        {
            "evidence_id": f"{run_id}-grade",
            "node_id": node_id,
            "check_class": "HIDDEN",
            "checks": checks,
            "versions": {
                "initial_outcomes": initial,
                "effective_outcomes": effective,
                "classification": classification,
            },
        },
        f"evidence-{run_id}",
    )


def _harness(tmp_path, name):
    store = EventStore(str(tmp_path / f"{name}.db"))
    controller = Controller(store, ScriptedExecutor({}))
    root = controller.submit_run(build_run_config(name, budgets={"MODEL_COST": 10.0}))
    return store, controller, root


def test_infrastructure_outcome_is_not_reported_as_fail(tmp_path):
    store, controller, root = _harness(tmp_path, "RUN-R13-INFRA")
    try:
        _admit_hidden(
            "RUN-R13-INFRA", controller,
            root,
            checks=[{"name": "hidden-parse", "passed": False, "detail": "initial=ORACLE_INFRASTRUCTURE_ERROR"}],
            initial={"hidden-parse": "ORACLE_INFRASTRUCTURE_ERROR"},
            effective={"hidden-parse": "ORACLE_INFRASTRUCTURE_ERROR"},
            classification="INDETERMINATE",
        )
        grades = observability.ObservabilityBackend(store).oracle_grades("RUN-R13-INFRA")
        assert grades[0]["outcome"] == "INDETERMINATE"
        assert grades[0]["effective_outcomes"] == {
            "hidden-parse": "ORACLE_INFRASTRUCTURE_ERROR"
        }
    finally:
        store.close()


def test_candidate_timeout_outcome_is_not_reported_as_fail(tmp_path):
    store, controller, root = _harness(tmp_path, "RUN-R13-TIMEOUT")
    try:
        _admit_hidden(
            "RUN-R13-TIMEOUT", controller,
            root,
            checks=[{"name": "hidden-parse", "passed": False, "detail": "initial=CANDIDATE_TIMEOUT"}],
            initial={"hidden-parse": "CANDIDATE_TIMEOUT"},
            effective={"hidden-parse": "CANDIDATE_TIMEOUT"},
            classification="INDETERMINATE",
        )
        grades = observability.ObservabilityBackend(store).oracle_grades("RUN-R13-TIMEOUT")
        assert grades[0]["outcome"] == "INDETERMINATE"
    finally:
        store.close()


def test_grade_outcome_still_reports_pass_and_fail(tmp_path):
    store, controller, root = _harness(tmp_path, "RUN-R13-PASSFAIL")
    try:
        _admit_hidden(
            "RUN-R13-PASSFAIL", controller,
            root,
            checks=[{"name": "hidden-parse", "passed": True}],
            initial={"hidden-parse": "PASS"},
            effective={"hidden-parse": "PASS"},
            classification="CORRECT_ACCEPTANCE",
        )
        grades = observability.ObservabilityBackend(store).oracle_grades("RUN-R13-PASSFAIL")
        assert grades[0]["outcome"] == "PASS"
        assert grades[0]["trial_classification"] == "CORRECT_ACCEPTANCE"
    finally:
        store.close()
