#!/usr/bin/env python3
"""Offline check for this tutorial fixture; not a Claude or Excel execution test."""
import csv,json,sys
from decimal import Decimal
from datetime import date
from pathlib import Path
p=Path(sys.argv[1]) if len(sys.argv)>1 else Path(__file__).with_name('tasks.csv')
reader=csv.DictReader(p.open(encoding='utf-8-sig',newline=''))
required={'task_id','task','owner','due_date','status','budget_usd','spent_usd'}
assert reader.fieldnames and required <= set(reader.fieldnames), 'Missing required header'
assert len(reader.fieldnames)==len(set(reader.fieldnames)), 'Duplicate header'
rows=list(reader)
ids=[r['task_id'] for r in rows]
assert len(rows)==6 and len(set(ids))==6 and set(ids)=={f'T00{i}' for i in range(1,7)}, 'Missing, duplicate or changed ID'
expected={
'T001':('120','100','Done','2026-11-10','Ana'),
'T002':('200','240','In progress','2026-11-11','Ben'),
'T003':('300','0','Not started','2026-11-14','Chen'),
'T004':('100','40','In progress','2026-11-12','Ana'),
'T005':('500','100','Blocked','2026-11-16','Ben'),
'T006':('80','0','Not started','2026-11-09','')}
names=dict(zip(['T001','T002','T003','T004','T005','T006'],['Brief','Copy','Design','Review','Launch','QA']))
for r in rows:
    assert r['task']==names[r['task_id']], 'Task name changed: '+r['task_id']
    b,s,status,d,owner=expected[r['task_id']]
    assert (Decimal(r['budget_usd']),Decimal(r['spent_usd']),r['status'],r['due_date'],r['owner'])==(Decimal(b),Decimal(s),status,d,owner), 'Source field changed: '+r['task_id']
asof=date(2026,11,12)
out={'scope':'synthetic fixture; offline Python, not Claude output','rows':len(rows),'budget_usd':str(sum(Decimal(r['budget_usd']) for r in rows)),'spent_usd':str(sum(Decimal(r['spent_usd']) for r in rows)),'remaining_usd':str(sum(Decimal(r['budget_usd'])-Decimal(r['spent_usd']) for r in rows)),'overdue_ids':sorted(r['task_id'] for r in rows if date.fromisoformat(r['due_date'])<asof and r['status']!='Done'),'missing_owner_ids':sorted(r['task_id'] for r in rows if not r['owner']),'overspent_ids':sorted(r['task_id'] for r in rows if Decimal(r['spent_usd'])>Decimal(r['budget_usd']))}
assert (out['budget_usd'],out['spent_usd'],out['remaining_usd'])==('1300','480','820')
assert out['overdue_ids']==['T002','T006'] and out['missing_owner_ids']==['T006'] and out['overspent_ids']==['T002']
print(json.dumps(out,indent=2))
