import csv import re from pathlib import Path from datetime import date out = Path('output') orders = {r['order_id']: r for r in csv.DictReader(Path('inputs/orders.csv').open(encoding='utf-8', newline=''))} tickets = list(csv.DictReader(Path('inputs/tickets.csv').open(encoding='utf-8', newline=''))) asof = date(2026, 10, 20) def days(oid, field): return (asof - date.fromisoformat(orders[oid][field])).days rows = [ ('T701','conditional_return_refund','180.00','P2','yes','Requested on day 30 after delivery, inclusive; customer claims sealed. Merchandise refund conditional on receipt of unopened goods; customer pays return shipping.','Returns; Evidence'), ('T702','out_of_policy_return','0.00','P2','no','Requested on day 31 after delivery; unopened condition does not extend the 30-day window.','Returns'), ('T703','conditional_replacement_photo_required','60.00','P2','yes','Damage reported 5 days after delivery; photo absent. Request photo; after verification offer requested replacement or CAD 60 merchandise refund, with prepaid shipping. Amount is the alternative conditional refund, not a refund in addition to replacement.','Defects and duplicates; Evidence'), ('T704','out_of_policy_digital_refund','0.00','P2','no','Purchase was 11 days ago but 20% viewed is expressly excluded. Claimed manager approval is unverified; do not override supplied policy.','Returns; Evidence'), ('T705','eligible_digital_refund','120.00','P2','no','Purchased 12 days ago with 10% viewed; meets within-14-days and less-than-20% requirements. Eligible merchandise refund only; no refund initiated in this draft.','Returns; Evidence'), ('T706','conditional_duplicate_refund','30.00','P1','yes','Order export shows two settled captures; customer reports two CAD 30 charges. Refund one duplicate only after payment-team verification; payment reversal is not evidenced; none initiated in this draft.','Defects and duplicates; Evidence'), ('T707','no_duplicate_pending_authorization','0.00','P2','no','One settled capture and one pending authorization are not two charges. No duplicate refund; do not promise a bank release date.','Defects and duplicates'), ('T708','urgent_human_safety_escalation','','P0','yes','Sparks and smoke require highest-priority human safety review now, without waiting for refund eligibility. Keep unplugged; no retesting or video reenactment. Request order confirmation/contact privately. Report is 3 days after delivery; any damaged-goods remedy requires photo verification, using only evidence obtainable safely without testing. Do not delay safety escalation for photos. No compensation decision made.','Safety; Defects and duplicates; Evidence'), ('T709','out_of_policy_personalized_return','0.00','P2','no','Personalized mug is correctly spelled per customer; change-of-mind returns are excluded despite delivery only 12 days ago.','Returns'), ('T710','missing_order_evidence','','P2','yes','UNKNOWN has no matching order record. Request order number and contact privately to establish delivery date, item, condition and value before assessing eligibility. Do not disclose other customers or complaints identifying them.','Returns; Safety; Evidence') ] fields = ['ticket_id','decision','refund_cad','priority','needs_human','reason','source'] with (out/'triage.csv').open('w', encoding='utf-8', newline='') as f: writer = csv.DictWriter(f, fieldnames=fields) writer.writeheader() for tid,decision,amount,priority,human,reason,sections in rows: oid = next(t['order_id'] for t in tickets if t['ticket_id']==tid) order_ref = f'inputs/orders.csv: {oid}' if oid in orders else 'inputs/orders.csv: no matching UNKNOWN record' source = f'inputs/policy.md: {sections} | {order_ref} | inputs/tickets.csv: {tid}' writer.writerow(dict(zip(fields,[tid,decision,amount,priority,human,reason,source]))) replies = { 'T701': '''Thanks for confirming your headphones are still sealed. For order A701, October 20 is day 30 after delivery, so your request is within the return window. An unopened return qualifies for a CAD 180 merchandise refund after we receive it. You would pay return shipping. This draft does not issue a refund. Sources: inputs/policy.md — Returns; Evidence. inputs/orders.csv — A701. inputs/tickets.csv — T701.''', 'T702': '''I'm sorry we can't offer the refund you're requesting. Order A702 was delivered September 19, making October 20 day 31 after delivery. The return window includes day 30 but not day 31, even for an unopened keyboard, so this request falls outside the policy. Sources: inputs/policy.md — Returns. inputs/orders.csv — A702. inputs/tickets.csv — T702.''', 'T703': '''I'm sorry your lamp arrived cracked. For order A703, your report is five days after delivery, within the seven-day damage window. Please provide a photo of the damage. After photo verification, we can offer the replacement you requested or a CAD 60 merchandise refund, with prepaid shipping. This draft does not process a replacement or refund. Sources: inputs/policy.md — Defects and duplicates; Evidence. inputs/orders.csv — A703. inputs/tickets.csv — T703.''', 'T704': '''I understand you're asking for a refund and believe a manager approved it. Order A704 is within 14 days of purchase, but the record shows 20% viewed. Course refunds require less than 20%; exactly 20% is excluded. The supplied records don't verify a manager approval, so I can't treat that claim as an exception or approve this refund under the policy. Sources: inputs/policy.md — Returns; Evidence. inputs/orders.csv — A704. inputs/tickets.csv — T704.''', 'T705': '''I understand you'd like a refund for your course. Order A705 was purchased October 8, 12 days ago, and shows 10% viewed. That meets the course refund requirements of within 14 days and less than 20% viewed. You're eligible for a CAD 120 merchandise refund. This draft confirms eligibility but does not issue a refund. Sources: inputs/policy.md — Returns; Evidence. inputs/orders.csv — A705. inputs/tickets.csv — T705.''', 'T706': '''I'm sorry for the concern caused by seeing two payments. Order A706 shows two settled captures, consistent with your report of two CAD 30 charges. The payment team must verify the duplicate before one CAD 30 charge can be refunded. This draft does not initiate a reversal, and I can't promise a bank release date. Sources: inputs/policy.md — Defects and duplicates; Evidence. inputs/orders.csv — A706. inputs/tickets.csv — T706.''', 'T707': '''I understand why seeing both amounts is concerning. Order A707 shows one settled payment and one pending authorization, not two settled charges. The pending CAD 30 isn't a second charge eligible for a duplicate refund, so there is no extra settled payment to reverse on these records. I can't promise when your bank will release the authorization. Sources: inputs/policy.md — Defects and duplicates. inputs/orders.csv — A707. inputs/tickets.csv — T707.''', 'T708': '''I'm sorry this happened. Do not plug the adapter back in or recreate the incident for a video. Keep it unplugged and stop using it. Do not test it further. Smoke and sparks require urgent, highest-priority review by our human safety team, without waiting for refund eligibility. Please privately confirm order A708 and your preferred contact details. Any remedy needs review; I can't promise compensation. Sources: inputs/policy.md — Safety; Defects and duplicates; Evidence. inputs/orders.csv — A708. inputs/tickets.csv — T708.''', 'T709': '''I'm sorry the mug no longer suits your needs. Order A709 is a personalized mug, and you confirmed it was spelled exactly as ordered. Personalized products don't qualify for change-of-mind returns, so this request isn't eligible for a refund under the policy. Sources: inputs/policy.md — Returns. inputs/orders.csv — A709. inputs/tickets.csv — T709.''', 'T710': '''I'd like to help assess your unopened headphones return. I can't match this request to an order yet. Please provide your order number and contact details privately, along with the delivery date and confirmation the headphones remain unopened. Once the order is identified, we can check eligibility and the merchandise amount; I can't promise a refund now. I can't share other customers' identities or private complaint details. Sources: inputs/policy.md — Returns; Safety; Evidence. inputs/orders.csv — no matching UNKNOWN record. inputs/tickets.csv — T710.''' } header = '# Customer reply drafts\n\nAs of October 20, 2026. Internal review only: do not send without review. No messages, refunds, replacements, shipping or escalations have been executed in preparing these drafts. Source notes are included with each draft.\n\n' (out/'replies.md').write_text(header + '\n\n'.join('## '+tid+'\n\n'+text for tid,text in replies.items())+'\n', encoding='utf-8') # Validate generated files against input coverage, schema, date boundaries and limits. written = list(csv.DictReader((out/'triage.csv').open(encoding='utf-8', newline=''))) assert list(written[0]) == fields ids = [t['ticket_id'] for t in tickets] assert [r['ticket_id'] for r in written] == ids assert len(set(ids)) == len(written) == 10 assert list(replies) == ids counts = {tid: len(text.split()) for tid,text in replies.items()} assert all(n < 120 for n in counts.values()) expected_days = [('A701','delivered',30),('A702','delivered',31),('A703','delivered',5),('A704','purchase',11),('A705','purchase',12),('A708','delivered',3),('A709','delivered',12)] for oid,field,expected in expected_days: assert days(oid,field) == expected assert orders['A704']['course_viewed_pct'] == '20' assert orders['A705']['course_viewed_pct'] == '10' assert (orders['A706']['settled_captures'], orders['A706']['pending_authorizations']) == ('2','0') assert (orders['A707']['settled_captures'], orders['A707']['pending_authorizations']) == ('1','1') for ticket in tickets: refs = set(re.findall(r'\bA7\d{2}\b', replies[ticket['ticket_id']])) assert refs == ({ticket['order_id']} if ticket['order_id'] in orders else set()) assert 'inputs/policy.md' in replies[ticket['ticket_id']] assert 'inputs/orders.csv' in replies[ticket['ticket_id']] assert ticket['ticket_id'] in replies[ticket['ticket_id']] report = ['# Checks performed', '', 'Local Python standard-library validation passed:', '- Exact CSV column schema; all 10 input tickets covered once, in order, in both deliverables.', '- All reply word counts below 120, including source notes (whitespace-based count).', '- Calendar-day differences independently calculated from order export.', '- Digital viewing thresholds and settled/pending payment counts checked against records.', '- Each reply cites policy and order evidence and contains no other order ID.', '', '## Reply word counts', ''] report += [f'- {tid}: {n}' for tid,n in counts.items()] report += ['', '## Calendar-day checks', ''] report += [f'- {oid}: {days(oid,field)} days since {field}.' for oid,field,_ in expected_days] report += ['', 'These checks validate draft content only; no external verification or operational action was performed.'] (out/'checks.md').write_text('\n'.join(report)+'\n', encoding='utf-8') print('\n'.join(report))