from gitea_client import GiteaClient, Issue, User


def _issue(number: int, assignees: list[str]) -> Issue:
    return Issue(
        number=number,
        title=f"Issue {number}",
        body="",
        state="open",
        user=User(id=1, login="Timmy"),
        assignees=[User(id=i + 10, login=name) for i, name in enumerate(assignees)],
        labels=[],
    )


def test_find_agent_issues_filters_actual_assignees(monkeypatch):
    client = GiteaClient(base_url="http://example.invalid", token="test-token")

    returned = [
        _issue(73, ["Timmy"]),
        _issue(74, ["gemini"]),
        _issue(75, ["grok", "Timmy"]),
        _issue(76, []),
    ]

    monkeypatch.setattr(client, "list_issues", lambda *args, **kwargs: returned)

    gemini_issues = client.find_agent_issues("Timmy_Foundation/timmy-config", "gemini")
    grok_issues = client.find_agent_issues("Timmy_Foundation/timmy-config", "grok")
    kimi_issues = client.find_agent_issues("Timmy_Foundation/timmy-config", "kimi")

    assert [issue.number for issue in gemini_issues] == [74]
    assert [issue.number for issue in grok_issues] == [75]
    assert kimi_issues == []


def test_find_agent_issues_is_case_insensitive(monkeypatch):
    client = GiteaClient(base_url="http://example.invalid", token="test-token")
    returned = [_issue(80, ["Gemini"])]
    monkeypatch.setattr(client, "list_issues", lambda *args, **kwargs: returned)

    issues = client.find_agent_issues("Timmy_Foundation/the-nexus", "gemini")

    assert [issue.number for issue in issues] == [80]
