{"files": {"main.py": "from core import run\nfrom policy import POLICY\ndef evaluate(payload):\n    return run(payload, POLICY)\n", "core.py": "from helpers import principal_cents, deduplicate_events, order_refund_events\n\ndef run(payload, policy):\n    if not payload:\n        return {\"invoices\": []}\n    if isinstance(payload, dict):\n        raw_invoices = payload.get(\"invoices\") or []\n    elif isinstance(payload, list):\n        raw_invoices = payload\n    else:\n        return {\"invoices\": []}\n    currency = policy.get(\"currency\")\n    min_invoice_cents = policy.get(\"min_invoice_cents\", 0)\n    fee_refundable = policy.get(\"fee_refundable\", False)\n    max_age = policy.get(\"max_refund_age_days\", 0)\n    inclusive = policy.get(\"inclusive_limit\", False)\n    overflow = policy.get(\"refund_overflow\", \"cap\")\n    tombstone = policy.get(\"void_is_tombstone\", False)\n    status_case = str(policy.get(\"status_case\", \"lower\")).lower()\n    order = policy.get(\"order\", \"id\")\n    rounding = policy.get(\"rounding\", \"per_line\")\n    duplicate_mode = policy.get(\"duplicate_event\", \"first\")\n    refunds_first = policy.get(\"refunds_first\", False)\n    results = []\n    for invoice in raw_invoices:\n        if not isinstance(invoice, dict):\n            continue\n        if invoice.get(\"currency\") != currency:\n            continue\n        invoice_id = invoice.get(\"id\")\n        created = invoice.get(\"created\", invoice.get(\"created_day\", invoice.get(\"day\", 0)))\n        lines = invoice.get(\"lines\") or []\n        fee = invoice.get(\"fee_cents\", 0) or 0\n        principal = principal_cents(lines, rounding)\n        charge = principal + int(fee)\n        if charge < min_invoice_cents:\n            continue\n        events = deduplicate_events(invoice.get(\"events\") or [], duplicate_mode)\n        events = order_refund_events(events, refunds_first)\n        active = True\n        tombstoned = False\n        refunded = 0\n        ceiling = charge if fee_refundable else principal\n        for event in events:\n            kind = event.get(\"kind\")\n            if kind == \"void\":\n                active = False\n                if tombstone:\n                    tombstoned = True\n            elif kind == \"restore\":\n                if not tombstoned:\n                    active = True\n            elif kind == \"refund\":\n                if not active:\n                    continue\n                event_day = event.get(\"day\", 0)\n                age = event_day - created\n                if age < 0:\n                    continue\n                if inclusive:\n                    if age > max_age:\n                        continue\n                else:\n                    if age >= max_age:\n                        continue\n                cents = int(event.get(\"cents\", event.get(\"amount_cents\", 0)) or 0)\n                remaining = ceiling - refunded\n                if remaining <= 0:\n                    continue\n                if overflow == \"cap\":\n                    refunded += min(cents, remaining)\n                else:\n                    if cents <= remaining:\n                        refunded += cents\n        if not active:\n            status = \"void\"\n        elif refunded == charge:\n            status = \"refunded\"\n        else:\n            status = \"open\"\n        if status_case == \"upper\":\n            status = status.upper()\n        record = {\"id\": invoice_id, \"charge\": charge, \"refunded\": refunded, \"balance\": charge - refunded, \"status\": status}\n        results.append((created, record))\n    if order == \"id\":\n        results.sort(key=lambda item: item[1][\"id\"])\n    else:\n        results.sort(key=lambda item: (item[0], item[1][\"id\"]))\n    return {\"invoices\": [record for _, record in results]}\n", "policy.py": "POLICY = {\"currency\": \"USD\", \"duplicate_event\": \"first\", \"fee_refundable\": True, \"inclusive_limit\": True, \"max_refund_age_days\": 30, \"min_invoice_cents\": 10, \"order\": \"id\", \"refund_overflow\": \"cap\", \"refunds_first\": True, \"rounding\": \"per_line\", \"status_case\": \"lower\", \"void_is_tombstone\": True}\n", "helpers.py": "from decimal import Decimal, ROUND_HALF_UP\n\ndef _to_decimal(value):\n    return Decimal(str(value))\n\ndef principal_cents(lines, rounding):\n    if rounding == \"per_line\":\n        total = 0\n        for line in lines:\n            cents = (_to_decimal(line) * Decimal(\"100\")).quantize(Decimal(\"1\"), rounding=ROUND_HALF_UP)\n            total += int(cents)\n        return total\n    total_dollars = sum((_to_decimal(line) for line in lines), Decimal(\"0\"))\n    cents = (total_dollars * Decimal(\"100\")).quantize(Decimal(\"1\"), rounding=ROUND_HALF_UP)\n    return int(cents)\n\ndef deduplicate_events(events, mode):\n    by_id = {}\n    for event in events:\n        eid = event.get(\"eid\")\n        if eid not in by_id:\n            by_id[eid] = event\n        elif mode == \"last\":\n            by_id[eid] = event\n    return list(by_id.values())\n\ndef order_refund_events(events, refunds_first):\n    if not refunds_first:\n        return list(events)\n    refunds = [event for event in events if event.get(\"kind\") == \"refund\"]\n    others = [event for event in events if event.get(\"kind\") != \"refund\"]\n    return refunds + others\n"}}