from common import *
import re,json
g,t=load();p=g[g.valid&~g.free&~g.explicit&g.year.between(2023,2025)].copy()
nouns='items|weapons|upgrades|abilities|passives|characters|classes|relics|cards|spells|perks|skills|units|heroes|artifacts|talents|augments|builds|traits|runes'
pat=re.compile(r'(?<![\d,])(\d{2,4})\s*\+?\s*(?:unique\s+|different\s+|distinct\s+|powerful\s+|collectible\s+|playable\s+|craftable\s+)*('+nouns+r')\b',re.I)
def quantity(s):
    cs={}
    for m in pat.finditer(s):
        n=int(m[1]);noun=m[2].lower()
        if not 10<=n<=5000 or noun=='units':continue
        context=s[max(0,m.start()-45):m.end()+45]
        if re.search(r'\b(?:monsters?|enemies)\b',context,re.I):continue
        cs[noun]=max(cs.get(noun,0),n)
    return max(cs.values(),default=0)
p['one_quantity']=p.desc.map(quantity)
rows=[]
for threshold in [10,100,300]:
    a=p[p.one_quantity.ge(threshold)];b=p[p.one_quantity.lt(threshold)]
    rows.append(dict(threshold=threshold,definition='largest_noun_count_exclude_units_enemy_context',**stats(a),**compare(a,b,cols=('year','price_band','primary','length_band'))))
save(rows,'quantity_definition_sensitivity.csv')
audit=[(1560250,'Not an option count: 1000 simultaneous units; 20 cavalry types is a separate claim.'),(2079680,'500 craftable items; content count, not demonstrated meaningful configurations.'),(2452820,'300 cards with drafting and combos.'),(2468100,'200 relics plus 100 cards; the 300 total combines unlike objects.'),(2508890,'Spells artifacts talents and total skills may overlap; cannot sum into unique choices.'),(2568620,'600 items of various types; actual build diversity unverified.'),(2758110,'Characters items and skills; counts combine unlike objects.'),(3054470,'Per-hero and total ability counts overlap; naive summation can double count.'),(3065150,'500 to 600 monster skills, not necessarily player build options.'),(3262610,'800 available cards; explicit deck construction.'),(3352240,'300 perks from eight classes; explicit build promise.'),(3372080,'600 items; configuration depth not established by the count.')]
(R/'quantity_description_audit.json').write_text(json.dumps({'selection':'12 cases sampled from original claim_total>=300 with random_state=706; prices and review outcomes not printed during description review','cases':[dict(appid=a,note=b) for a,b in audit]},indent=2))
print('Quantity definition sensitivity complete')
