"""Backfill readiness: a project is READY only with one owner, no blocks, and Cloud links."""

from src.domain.project_auth.backfill import BLOCKED, READY, _classify
from src.domain.project_auth.membership_reconciliation import Plan


def _plan(blocked=()):
    return Plan(operations=(), blocked=tuple(blocked))


def test_ready_when_one_owner_no_blocks_and_every_identity_linked():
    cls, reasons = _classify(["o@x.test"], ["o@x.test", "a@x.test"],
                             {"o@x.test": "usr_o", "a@x.test": "usr_a"}, _plan())
    assert cls == READY and reasons == ()


def test_blocked_without_a_cloud_user_for_every_identity():
    cls, reasons = _classify(["o@x.test"], ["o@x.test", "a@x.test"],
                             {"o@x.test": "usr_o"}, _plan())
    assert cls == BLOCKED
    assert "cloud_user_missing:a@x.test" in reasons


def test_blocked_on_zero_or_multiple_owners():
    assert "desired_owner_count_0" in _classify([], ["a@x.test"], {"a@x.test": "u"}, _plan())[1]
    assert "desired_owner_count_2" in _classify(
        ["o@x.test", "p@x.test"], ["o@x.test", "p@x.test"],
        {"o@x.test": "u1", "p@x.test": "u2"}, _plan())[1]


def test_planner_blocks_propagate():
    cls, reasons = _classify(["o@x.test"], ["o@x.test"], {"o@x.test": "u"},
                             _plan(["legacy_hold_requires_reinstatement:o@x.test"]))
    assert cls == BLOCKED and "legacy_hold_requires_reinstatement:o@x.test" in reasons
