"""PAPER-06 — the manual-quarantine CLI is wired, not a dead API.

``QuarantineStore.force_quarantine()`` / ``force_evict()`` cited
``--quarantine-add`` / ``--quarantine-remove`` in their docstrings while
the argparse parser wired neither. These tests pin the wiring: the flags
exist, mutate ``.gates/quarantine.json`` through the store, and a
remove of a not-quarantined id exits 3 (usage error) so a typo can't
masquerade as a fix.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

from mgf.test_supervisor.__main__ import main
from mgf.test_supervisor.quarantine import QuarantineStore

if TYPE_CHECKING:
    from pathlib import Path

TEST_ID = "tests/test_x.py::test_flaky"


@pytest.fixture
def project_root(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
    (tmp_path / "pyproject.toml").write_text("[tool.pytest.ini_options]\n", encoding="utf-8")
    monkeypatch.setenv("MGF_TEST_SUPERVISOR_REPO_ROOT", str(tmp_path))
    monkeypatch.delenv("MGF_GATES_DIR", raising=False)
    return tmp_path


def _store(root: Path) -> QuarantineStore:
    return QuarantineStore(root / ".gates" / "quarantine.json")


def test_quarantine_add_force_quarantines_with_reason(project_root: Path) -> None:
    rc = main(["--quarantine-add", TEST_ID, "--reason", "known flaky under load"])
    assert rc == 0
    entry = _store(project_root).all().get(TEST_ID)
    assert entry is not None
    assert entry.quarantined_at, "must be QUARANTINED, not merely watched"
    assert entry.last_failure_summary == "known flaky under load"


def test_quarantine_add_default_reason(project_root: Path) -> None:
    assert main(["--quarantine-add", TEST_ID]) == 0
    entry = _store(project_root).all()[TEST_ID]
    assert entry.last_failure_summary == "manually quarantined via --quarantine-add"


def test_quarantine_remove_evicts(project_root: Path) -> None:
    assert main(["--quarantine-add", TEST_ID]) == 0
    assert _store(project_root).is_quarantined(TEST_ID)
    assert main(["--quarantine-remove", TEST_ID]) == 0
    assert not _store(project_root).is_quarantined(TEST_ID)


def test_quarantine_remove_unknown_id_is_usage_error(
    project_root: Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    assert main(["--quarantine-remove", TEST_ID]) == 3
    assert "not quarantined" in capsys.readouterr().err
