"""Tests for the `reviewer_id` "my reviews" queue filter on GET /tests.

Block 3 fix: red_lead/blue_lead had no way to see tests specifically
assigned to THEM at the red_review/blue_review lead-review gate — the
existing `pending_validation_side` filter only covers the later, unrelated
in_review manager-validation stage and ignores assignment entirely.
"""

import uuid

from app.models.enums import TestState
from app.models.technique import Technique
from app.models.test import Test
from app.services.test_crud_service import list_tests


def _make_test(db, owner_id, state, name, red_reviewer=None, blue_reviewer=None):
    tech = Technique(mitre_id=f"T9{uuid.uuid4().hex[:6]}", name="X", tactic="execution", platforms=["windows"])
    db.add(tech)
    db.flush()
    test = Test(
        technique_id=tech.id,
        name=name,
        state=state,
        created_by=owner_id,
        red_reviewer_assignee=red_reviewer,
        blue_reviewer_assignee=blue_reviewer,
    )
    db.add(test)
    db.commit()
    db.refresh(test)
    return test


def test_reviewer_id_with_explicit_state_filters_by_matching_assignee(db, red_lead_user, blue_lead_user):
    mine = _make_test(db, red_lead_user.id, TestState.red_review, "Mine", red_reviewer=red_lead_user.id)
    _make_test(db, red_lead_user.id, TestState.red_review, "Someone else's", red_reviewer=blue_lead_user.id)

    results = list_tests(db, state="red_review", reviewer_id=red_lead_user.id)

    assert [t.id for t in results] == [mine.id]


def test_reviewer_id_without_state_covers_both_red_and_blue_review(db, red_lead_user, blue_lead_user):
    red = _make_test(db, red_lead_user.id, TestState.red_review, "Red review mine", red_reviewer=red_lead_user.id)
    blue = _make_test(db, red_lead_user.id, TestState.blue_review, "Blue review mine", blue_reviewer=red_lead_user.id)
    _make_test(db, red_lead_user.id, TestState.red_review, "Not mine", red_reviewer=blue_lead_user.id)
    _make_test(db, red_lead_user.id, TestState.draft, "Unrelated state")

    results = list_tests(db, reviewer_id=red_lead_user.id)

    assert {t.id for t in results} == {red.id, blue.id}


def test_reviewer_id_excludes_unassigned_tests_in_that_state(db, red_lead_user):
    _make_test(db, red_lead_user.id, TestState.red_review, "Unassigned", red_reviewer=None)

    results = list_tests(db, state="red_review", reviewer_id=red_lead_user.id)

    assert results == []
