"""Tests for pick_default_comment_target — chair-comment routing.

Regression guard for the recurring bug where a chair instruction typed into the
"כתוב הוראה לסוכנים..." box was routed to a *cancelled* child issue, so
Paperclip skipped the wakeup and the instruction was silently swallowed.

The picker must never default to a closed (done/cancelled) issue and must prefer
the live top-level CEO issue. Pure function — no DB, no FastAPI.
"""

from __future__ import annotations

import os
import sys
from pathlib import Path

import pytest

os.environ.setdefault("PAPERCLIP_DB_URL", "postgres://x:x@127.0.0.1:54329/paperclip")
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))  # web/

pc = pytest.importorskip("paperclip_client", reason="web deps unavailable")


def _iss(id_, status, parent_id=None):
    return {"id": id_, "status": status, "parent_id": parent_id, "identifier": id_}


def test_prefers_open_top_level_over_newer_cancelled_child():
    """The exact 1027-04-26 scenario: newest non-done issue is a cancelled child;
    routing must still land on the live top-level CEO issue."""
    issues = [
        _iss("main-old", "done"),                       # oldest top-level, closed
        _iss("main-live", "in_review"),                 # live CEO main issue
        _iss("child-cancelled", "cancelled", "main-live"),  # newest non-done → the trap
        _iss("child-done", "done", "main-live"),
    ]
    assert pc.pick_default_comment_target(issues)["id"] == "main-live"


def test_never_returns_cancelled_when_an_open_issue_exists():
    issues = [
        _iss("a", "done"),
        _iss("b", "in_progress", "a"),
        _iss("c", "cancelled", "a"),
    ]
    assert pc.pick_default_comment_target(issues)["status"] != "cancelled"


def test_falls_back_to_open_child_when_no_open_top_level():
    issues = [
        _iss("main", "done"),
        _iss("child-live", "in_progress", "main"),
    ]
    assert pc.pick_default_comment_target(issues)["id"] == "child-live"


def test_prefers_top_level_when_everything_closed():
    """No open issue anywhere — pick the newest top-level (better than a child)."""
    issues = [
        _iss("main-1", "done"),
        _iss("main-2", "cancelled"),
        _iss("child", "done", "main-2"),
    ]
    assert pc.pick_default_comment_target(issues)["id"] == "main-2"


def test_single_issue():
    issues = [_iss("only", "cancelled")]
    assert pc.pick_default_comment_target(issues)["id"] == "only"


def test_picks_newest_open_top_level():
    issues = [
        _iss("main-1", "in_review"),
        _iss("main-2", "in_review"),
    ]
    assert pc.pick_default_comment_target(issues)["id"] == "main-2"
