"""Amount-driven approval routing for requisitions.

A requisition's total picks an ordered chain of RBAC roles that must approve,
each role becoming one Approval row. Edit THRESHOLDS to retune the ladder; the
roles below must exist as RBAC Role slugs (see core seed: Procurement ->
"procurement", FinanceManager -> "finance-manager", SuperAdmin -> "super-admin").

Currency is IDR (Rupiah), matching the rest of procurement.
"""
from decimal import Decimal

# Role slugs, lowest authority first.
ROLE_MANAGER = "procurement"
ROLE_FINANCE = "finance-manager"
ROLE_DIRECTOR = "super-admin"

# Ordered by ascending ceiling. The first band whose `max` covers the amount
# wins. `max = None` is the catch-all top band. `chain = []` means auto-approve
# (no human step needed). Amounts are an inclusive lower / exclusive upper read
# as "amount < max".
THRESHOLDS = [
    {"max": Decimal("1000000"),  "chain": []},                                   # < 1M  → auto
    {"max": Decimal("10000000"), "chain": [ROLE_MANAGER]},                       # < 10M → manager
    {"max": Decimal("50000000"), "chain": [ROLE_MANAGER, ROLE_FINANCE]},         # < 50M → + finance
    {"max": None,                "chain": [ROLE_MANAGER, ROLE_FINANCE, ROLE_DIRECTOR]},  # ≥ 50M → + director
]


def chain_for(amount) -> list[str]:
    """Return the ordered list of role slugs required to approve `amount`.

    An empty list means the requisition auto-approves (below the floor). A null
    or zero amount is treated as the lowest band (auto-approve) — callers that
    require an explicit amount should validate before submitting.
    """
    value = Decimal(str(amount)) if amount is not None else Decimal("0")
    for band in THRESHOLDS:
        if band["max"] is None or value < band["max"]:
            return list(band["chain"])
    return list(THRESHOLDS[-1]["chain"])  # unreachable, kept for safety
