"""Fixed fictional offers; salary/territory screen, never a permit approval.
Policy date: applications from 2026-06-01, read on 2026-10-28.
Whole monthly RM only. The employer's sector is GIVEN, not inferred here.
Usage: python3 verify.py [offers.csv]
"""
import csv
import json
import pathlib
import sys


def screen(basic, sector, territory="Peninsular"):
    if not isinstance(basic, int) or isinstance(basic, bool) or basic < 0:
        raise ValueError("basic must be a nonnegative integer")
    if territory != "Peninsular":
        return "outside_scope"
    if basic >= 20000:
        return "I"
    if basic >= 10000:
        return "II"
    if sector not in ("other_confirmed", "manufacturing_confirmed"):
        return "sector_unconfirmed"
    floor = 7000 if sector == "manufacturing_confirmed" else 5000
    return "III" if basic >= floor else "below_applicable_floor"


def verify(path):
    with path.open(newline="", encoding="utf-8") as stream:
        rows = list(csv.DictReader(stream))
    fixed = [
        ("A", 8500, 2500, "III", 11000),
        ("B", 12000, 500, "II", 12500),
        ("C", 6500, 1500, "below_applicable_floor", 8000),
    ]
    actual = []
    for row in rows:
        basic, allowance = int(row["basic"]), int(row["allowance"])
        if allowance < 0:
            raise ValueError("negative allowance")
        actual.append((row["offer"], basic, allowance,
                       screen(basic, row["sector"], row["territory"]),
                       basic + allowance))
    assert actual == fixed, "fixed fictional offers differ"
    # Literal expected boundaries independently selected from the policy table.
    edges = [(20000, "other_confirmed", "I"),
             (19999, "other_confirmed", "II"),
             (10000, "other_confirmed", "II"),
             (9999, "other_confirmed", "III"),
             (7000, "manufacturing_confirmed", "III"),
             (6999, "manufacturing_confirmed", "below_applicable_floor"),
             (5000, "other_confirmed", "III"),
             (4999, "other_confirmed", "below_applicable_floor")]
    for basic, sector, expected in edges:
        assert screen(basic, sector) == expected
    assert screen(12000, "other_confirmed", "Sarawak") == "outside_scope"
    assert screen(8500, "unknown") == "sector_unconfirmed"
    for bad in (-1, 1.5, True):
        try:
            screen(bad, "other_confirmed")
        except ValueError:
            pass
        else:
            raise AssertionError("invalid input accepted")
    return {"offers": actual, "boundaries_checked": len(edges),
            "scope": "Salary screen only; employer, position, documents and individual approval remain unchecked"}


if __name__ == "__main__":
    try:
        source = pathlib.Path(sys.argv[1]) if len(sys.argv) > 1 else pathlib.Path(__file__).with_name("offers.csv")
        print(json.dumps(verify(source), ensure_ascii=False, indent=2))
    except (ValueError, AssertionError, KeyError) as error:
        print("FAIL: " + str(error), file=sys.stderr)
        sys.exit(1)
