{"files": {"main.py": "from core import run\nfrom policy import POLICY\ndef evaluate(payload):\n    return run(payload, POLICY)\n", "policy.py": "POLICY = {\n    'timezone': 'America/New_York',\n    'cutoff_hour': 17,\n    'cutoff_inclusive': True,\n    'lead_minutes': 30,\n    'adjacency_allowed': True,\n    'duration_max': 120,\n    'cancelled_blocks': True,\n    'duplicate_policy': 'first',\n    'capacity': 1,\n    'report_order': 'id',\n    'blackout_day': '2026-11-01',\n    'priority_high_first': True,\n}\n", "helpers.py": "from datetime import datetime, timezone, timedelta\n\ndef parse_dt(s):\n    if isinstance(s, datetime):\n        return s\n    if isinstance(s, str) and s.endswith('Z'):\n        s = s[:-1] + '+00:00'\n    return datetime.fromisoformat(s)\n\ndef to_utc(dt):\n    return dt.astimezone(timezone.utc)\n\ndef overlaps(a, b, adjacency_allowed):\n    if adjacency_allowed:\n        return a[0] < b[1] and b[0] < a[1]\n    return a[0] <= b[1] and b[0] <= a[1]\n", "core.py": "from datetime import datetime, timedelta, timezone\nfrom zoneinfo import ZoneInfo\nfrom helpers import parse_dt, to_utc, overlaps\n\n\ndef run(payload, policy):\n    tz = ZoneInfo(policy['timezone'])\n    now = to_utc(parse_dt(payload['now']))\n    rows = payload.get('appointments', [])\n    first_pos = {}\n    kept = {}\n    for i, a in enumerate(rows):\n        k = a['id']\n        if k not in first_pos:\n            first_pos[k] = len(first_pos)\n            kept[k] = (i, a)\n        elif policy['duplicate_policy'] == 'last':\n            kept[k] = (i, a)\n    items = sorted(kept.values(), key=lambda t: (first_pos[t[1]['id']]))\n    sign = -1 if policy['priority_high_first'] else 1\n    order = sorted(items, key=lambda t: (sign * t[1]['priority'], t[0]))\n    selected, blocked, rejected = [], [], []\n    accepted = []\n    lead = timedelta(minutes=policy['lead_minutes'])\n    for idx, a in order:\n        aid = a['id']\n        ok = True\n        try:\n            dur = a['duration']\n            if isinstance(dur, bool) or not dur > 0 or dur > policy['duration_max']:\n                ok = False\n            sdt = parse_dt(a['start'])\n            s = to_utc(sdt)\n            if ok and s < now + lead:\n                ok = False\n            if ok:\n                e = s + timedelta(minutes=dur)\n                ls = s.astimezone(tz)\n                if ls.date().isoformat() == policy['blackout_day']:\n                    ok = False\n                else:\n                    cut = datetime(ls.year, ls.month, ls.day, policy['cutoff_hour'], 0, tzinfo=tz)\n                    cut = to_utc(cut)\n                    if policy['cutoff_inclusive']:\n                        ok = e <= cut\n                    else:\n                        ok = e < cut\n            if ok and a.get('cancelled') and not policy['cancelled_blocks']:\n                ok = False\n            if ok:\n                iv = (s, e)\n                cnt = sum(1 for x in accepted if overlaps(iv, x, policy['adjacency_allowed']))\n                if cnt >= policy['capacity']:\n                    ok = False\n        except Exception:\n            ok = False\n        if not ok:\n            rejected.append((idx, aid))\n            continue\n        accepted.append(iv)\n        if a.get('cancelled'):\n            blocked.append((idx, aid))\n        else:\n            selected.append((idx, aid))\n\n    def fmt(lst):\n        if policy['report_order'] == 'id':\n            return sorted(x[1] for x in lst)\n        return [x[1] for x in sorted(lst, key=lambda x: x[0])]\n    return {'selected': fmt(selected), 'blocked': fmt(blocked), 'rejected': fmt(rejected)}\n"}}