"""Small deterministic reference builds. No AI calls, messages or CRM writes."""
from copy import deepcopy

def enquiry_queue(records):
 out=[];seen=set()
 for source in records:
  row=deepcopy(source);key=row.get('id')
  if not key or key in seen:continue
  seen.add(key)
  intent=row.get('intent') if row.get('intent') in ('valuation','viewing','landlord','other') else 'UNKNOWN'
  out.append({'id':key,'intent':intent,'property':row.get('property') or 'UNKNOWN','contact_permission':row.get('contact_permission') if row.get('contact_permission') in ('yes','no') else 'UNKNOWN','owner':row.get('owner') or 'UNASSIGNED','status':'STAFF_REVIEW','send':False})
 return out

def vendor_update(records):
 unique={}
 for row in records:
  if row.get('id'):unique.setdefault(row['id'],deepcopy(row))
 return {'viewings':len(unique),'feedback':[{'id':k,'note':v.get('feedback') or 'UNKNOWN'} for k,v in unique.items()], 'offers':[{'id':k,'amount':v['offer']} for k,v in unique.items() if isinstance(v.get('offer'),(int,float)) and not isinstance(v.get('offer'),bool) and v['offer']>0], 'status':'DRAFT_FOR_STAFF_REVIEW','send':False}

def progression_handover(records):
 rows=[deepcopy(r) for r in records if r.get('id') and r.get('date')]
 rows.sort(key=lambda r:(r['date'],r['id']))
 return {'timeline':[{'id':r['id'],'date':r['date'],'fact':r.get('fact') or 'UNKNOWN'} for r in rows], 'unresolved':[{'id':r['id'],'owner':r.get('owner') or 'UNASSIGNED','next_action':r.get('next_action') or 'UNKNOWN'} for r in rows if r.get('resolved') is not True], 'exchange_date':'UNKNOWN','status':'STAFF_REVIEW','send':False}

KITS={
 'enquiry':{'function':enquiry_queue,'sample':[{'id':'E-101','intent':'valuation','property':'Fictional House','owner':'Branch team'},{'id':'E-101','intent':'viewing'},{'id':'E-102','intent':'other','contact_permission':'no'}], 'scope':'Deduplication, explicit-intent routing, missing values and draft-only status. No AI output quality, CRM integration or live customer trial has been tested.'},
 'vendor':{'function':vendor_update,'sample':[{'id':'V-101','feedback':'Fictional viewer liked the garden.'},{'id':'V-102','feedback':'Fictional viewer asked about parking.','offer':250000},{'id':'V-102','offer':260000}], 'scope':'Unique viewing counts, explicitly supplied offers and draft-only status. No AI output quality, property valuation or live vendor trial has been tested.'},
 'progression':{'function':progression_handover,'sample':[{'id':'S-102','date':'2026-10-21','fact':'Fictional survey appointment requested.','next_action':'Confirm appointment','owner':'Progressor'},{'id':'S-101','date':'2026-10-20','fact':'Fictional offer accepted.','resolved':True}], 'scope':'Chronological ordering, unresolved-task ownership, unknown dates and draft-only status. No AI output quality, legal interpretation or live sales system has been tested.'},
}

def verify_kit(key):
 kit=KITS[key];sample=deepcopy(kit['sample']);out=kit['function'](sample)
 assert sample==kit['sample'],'input_mutated'
 assert kit['function']([]) is not None
 if key=='enquiry':
  assert len(out)==2 and out[0]['contact_permission']=='UNKNOWN'
  assert out[1]['contact_permission']=='no' and all(r['send'] is False for r in out)
 elif key=='vendor':
  assert out['viewings']==2 and out['offers']==[{'id':'V-102','amount':250000}]
  assert out['send'] is False and kit['function']([{'id':'x','offer':True}])['offers']==[]
 else:
  assert out['timeline'][0]['id']=='S-101' and len(out['unresolved'])==1
  assert out['exchange_date']=='UNKNOWN' and out['send'] is False
 return {'checks_passed':4,'method':'Deterministic Python reference logic on fictional data','scope':kit['scope'],'sample_input':sample,'sample_output':out}

if __name__=='__main__':
 import json
 for name in KITS:print(json.dumps({'kit':name,**verify_kit(name)},indent=2))
