"""Toy eligibility-before-ranking example; invented internal follow-up rules, no embeddings."""
import json
from datetime import date,timedelta
q={'market':'UK','product':'refurbished','dispatch':'2024-12-12','permittedDocuments':{'P1','P2','P3'}}
docs=[
{'id':'P1','market':'UK','product':'new','from':'2024-11-01','until':None,'days':30,'rankScore':9,'text':'UK new-product follow-up: schedule 30 calendar days after dispatch.'},
{'id':'P2','market':'UK','product':'refurbished','from':'2024-11-01','until':None,'days':14,'rankScore':5,'text':'UK refurbished follow-up: schedule 14 calendar days after dispatch. Pilot orders require manual coordination.'},
{'id':'P3','market':'OTHER','product':'refurbished','from':'2024-01-01','until':'2024-11-01','days':30,'rankScore':10,'text':'Archived other-market refurbished follow-up: schedule 30 calendar days after dispatch.'}]
rows=[]
for d in docs:
 checks={'access':d['id'] in q['permittedDocuments'],'market':d['market']==q['market'],'product':d['product']==q['product'],'effective':d['from']<=q['dispatch'] and (d['until'] is None or q['dispatch']<d['until'])}
 rows.append({'id':d['id'],**checks,'eligible':all(checks.values()),'illustrativeRankScore':d['rankScore']})
eligible=[d for d,r in zip(docs,rows) if r['eligible']];selected=max(eligible,key=lambda d:d['rankScore'])
unfiltered=max(docs,key=lambda d:d['rankScore'])
assert selected['id']=='P2' and unfiltered['id']=='P3'
followup=(date.fromisoformat(q['dispatch'])+timedelta(days=selected['days'])).isoformat();assert followup=='2024-12-26'
top_two=sorted(docs,key=lambda d:d['rankScore'],reverse=True)[:2]
postfiltered=[d['id'] for d in top_two if next(r['eligible'] for r in rows if r['id']==d['id'])]
assert postfiltered==[]
report={'topTwoThenFilter':postfiltered,'status':'Invented internal follow-up policies; rank scores assigned, not measured search results','query':{**q,'permittedDocuments':sorted(q['permittedDocuments'])},'documents':docs,'eligibility':rows,'unfilteredTop':'P3','eligibleTop':'P2','ordinaryFollowup':followup,'pilotOrder':'manual coordination; do not apply ordinary date automatically'}
if __name__=='__main__':print(json.dumps(report,indent=2))
