# retoor <retoor@molodetz.nl>

from devplacepy.routers.admin._shared import is_senior_admin, seniority_key

SENIOR_ADMIN = {
    "uid": "senior",
    "role": "Admin",
    "created_at": "2024-01-01T00:00:00+00:00",
    "id": 1,
}
JUNIOR_ADMIN = {
    "uid": "junior",
    "role": "Admin",
    "created_at": "2025-01-01T00:00:00+00:00",
    "id": 9,
}
OLD_MEMBER = {
    "uid": "member",
    "role": "Member",
    "created_at": "2020-01-01T00:00:00+00:00",
    "id": 0,
}


def test_junior_admin_cannot_manage_senior_admin():
    assert is_senior_admin(JUNIOR_ADMIN, SENIOR_ADMIN) is True


def test_senior_admin_can_manage_junior_admin():
    assert is_senior_admin(SENIOR_ADMIN, JUNIOR_ADMIN) is False


def test_members_are_never_protected():
    assert is_senior_admin(JUNIOR_ADMIN, OLD_MEMBER) is False
    assert is_senior_admin(SENIOR_ADMIN, OLD_MEMBER) is False


def test_acting_on_self_is_not_protected():
    assert is_senior_admin(SENIOR_ADMIN, SENIOR_ADMIN) is False
    assert is_senior_admin(JUNIOR_ADMIN, JUNIOR_ADMIN) is False


def test_none_target_is_not_protected():
    assert is_senior_admin(SENIOR_ADMIN, None) is False


def test_equal_created_at_tiebreaks_on_id():
    earlier = {"uid": "e1", "role": "Admin", "created_at": "2024-01-01T00:00:00", "id": 3}
    later = {"uid": "e2", "role": "Admin", "created_at": "2024-01-01T00:00:00", "id": 5}
    assert is_senior_admin(later, earlier) is True
    assert is_senior_admin(earlier, later) is False


def test_missing_created_at_sorts_oldest_failsafe():
    no_date = {"uid": "nc", "role": "Admin", "created_at": None, "id": 2}
    assert is_senior_admin(SENIOR_ADMIN, no_date) is True


def test_seniority_key_orders_by_created_at_then_id():
    assert seniority_key(SENIOR_ADMIN) < seniority_key(JUNIOR_ADMIN)
    assert seniority_key({"created_at": None, "id": 0}) == ("", 0)
