{"files":{"main.py":"from core import run\nfrom policy import POLICY\ndef evaluate(payload):\n    return run(payload, POLICY)\n","core.py":"from datetime import datetime, timedelta, timezone\nfrom zoneinfo import ZoneInfo\n\n\ndef _dt(s):\n    if isinstance(s, str) and s.endswith('Z'):\n        s = s[:-1] + '+00:00'\n    return datetime.fromisoformat(s)\n\n\ndef run(payload, policy):\n    zone = ZoneInfo(policy['timezone'])\n    now = _dt(payload['now'])\n    rows = list(payload.get('appointments', []))\n    pos = {}\n    chosen = {}\n    for i, a in enumerate(rows):\n        k = a['id']\n        if k not in pos:\n            pos[k] = len(pos)\n            chosen[k] = (i, a)\n        elif policy['duplicate_policy'] == 'last':\n            chosen[k] = (i, a)\n    items = [(idx, a) for idx, a in chosen.values()]\n    hi = policy['priority_high_first']\n    items.sort(key=lambda t: ((-t[1]['priority'] if hi else t[1]['priority']), t[0]))\n    min_start = now + timedelta(minutes=policy['lead_minutes'])\n    cap = policy['capacity']\n    adj = policy['adjacency_allowed']\n    accepted = []  # (start,end)\n    sel, blk, rej = [], [], []\n    for idx, a in items:\n        ok = True\n        if a.get('cancelled') and not policy['cancelled_blocks']:\n            ok = False\n        dur = a.get('duration')\n        if ok and (isinstance(dur, bool) or not isinstance(dur, int) or dur <= 0 or dur > policy['duration_max']):\n            ok = False\n        if ok:\n            start = _dt(a['start'])\n            utc = timezone.utc\n            su = start.astimezone(utc)\n            eu = su + timedelta(minutes=dur)\n            if su < min_start.astimezone(utc):\n                ok = False\n            else:\n                local = start.astimezone(zone)\n                if local.date().isoformat() == policy['blackout_day']:\n                    ok = False\n                else:\n                    cutoff = datetime(local.year, local.month, local.day, policy['cutoff_hour'], tzinfo=zone)\n                    cu = cutoff.astimezone(utc)\n                    if policy['cutoff_inclusive']:\n                        ok = eu <= cu\n                    else:\n                        ok = eu < cu\n        if ok:\n            cnt = 0\n            for s2, e2 in accepted:\n                if adj:\n                    o = su < e2 and s2 < eu\n                else:\n                    o = su <= e2 and s2 <= eu\n                if o:\n                    cnt += 1\n            if cnt >= cap:\n                ok = False\n        if ok:\n            accepted.append((su, eu))\n            (blk if a.get('cancelled') else sel).append((idx, a['id']))\n        else:\n            rej.append((idx, a['id']))\n\n    def out(lst):\n        if policy['report_order'] == 'id':\n            try:\n                lst = sorted(lst, key=lambda t: t[1])\n            except TypeError:\n                lst = sorted(lst, key=lambda t: str(t[1]))\n        else:\n            lst = sorted(lst, key=lambda t: t[0])\n        return [t[1] for t in lst]\n\n    return {'selected': out(sel), 'blocked': out(blk), 'rejected': out(rej)}\n","policy.py":"POLICY = {\n    'timezone': 'UTC',\n    'cutoff_hour': 18,\n    'cutoff_inclusive': False,\n    'lead_minutes': 60,\n    'adjacency_allowed': False,\n    'duration_max': 90,\n    'duplicate_policy': 'last',\n    'cancelled_blocks': False,\n    'blackout_day': '2026-11-02',\n    'priority_high_first': False,\n    'capacity': 2,\n    'report_order': 'input',\n}\n","helpers.py":"# Optional pure helper functions.\n"}}