from __future__ import annotations

from recoil.pipeline.tools.autonomy import readiness


def _body() -> str:
    return """# Goal
Build the requested queue filter.

## Acceptance Criteria
- Add the queue filter control.
- Verify the filtered list updates when the control changes.
- Tests pass with pytest.

## Verification
- Run python3 -m pytest recoil/pipeline/tools/tests/test_queue.py -q.

## Scope
- recoil/pipeline/tools/queue.py

## Out of Scope
- No billing or account changes.
"""


def _issue(**overrides):
    base = {
        "title": "Queue filter",
        "body": _body(),
        "labels": ["autonomy-ok"],
        "paths": ["recoil/pipeline/tools/queue.py"],
    }
    base.update(overrides)
    return base


def test_full_bodied_autonomy_ok_issue_is_ready():
    ok, reasons = readiness.is_ready(_issue())

    assert ok is True
    assert reasons == []


def test_missing_acceptance_criteria_is_not_ready():
    body = _body().replace("## Acceptance Criteria\n", "## Criteria\n")

    ok, reasons = readiness.is_ready(_issue(body=body))

    assert ok is False
    assert any("Acceptance Criteria" in reason for reason in reasons)


def test_control_plane_path_is_not_ready():
    ok, reasons = readiness.is_ready(
        _issue(paths=["recoil/pipeline/tools/autonomy/tick.py"])
    )

    assert ok is False
    assert any("control-plane" in reason for reason in reasons)


def test_paid_media_without_spend_ok_is_not_ready():
    issue = _issue(body=_body() + "\nThis requires paid media budget for ads.\n")

    ok, reasons = readiness.is_ready(issue)

    assert ok is False
    assert any("spend-ok" in reason for reason in reasons)


def test_paid_media_with_spend_ok_is_ready():
    issue = _issue(
        body=_body() + "\nThis requires paid media budget for ads.\n",
        labels=["autonomy-ok", "spend-ok"],
    )

    ok, reasons = readiness.is_ready(issue)

    assert ok is True
    assert reasons == []


def test_label_and_pr_exclusions():
    ok, reasons = readiness.is_ready(
        _issue(labels=["autonomy-ok", "autonomy-claimed"], open_pr=True)
    )

    assert ok is False
    assert any("autonomy-claimed" in reason for reason in reasons)
    assert any("open PR" in reason for reason in reasons)


def test_taste_only_acceptance_criteria_is_not_ready():
    body = """Goal:
Improve the dashboard.

Acceptance Criteria:
- It looks elegant.
- The result feels premium.

Verification:
- Review the screen.

Scope:
- recoil/pipeline/tools/dashboard.py

Out of Scope:
- No unrelated work.
"""

    ok, reasons = readiness.is_ready(_issue(body=body))

    assert ok is False
    assert any("not testable" in reason for reason in reasons)
