#!/usr/bin/env python3
"""Apply RankMath SEO meta (title + description) to posts/terms via rankmath/v1/updateMeta,
then verify by live-scraping each URL. Backup must already be taken. Usage:
   python3 apply_seo.py test     -> apply+verify ONE category (Hand Pallet Truck)
   python3 apply_seo.py rest     -> apply+verify the remaining 18 categories
"""
import sys, json, base64, urllib.request, ssl, re, html, time

USER='technolift'; APPPASS='GQ7b OxFc BR4p CL67 bftU CzEb'
ctx=ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
AUTH='Basic '+base64.b64encode(f'{USER}:{APPPASS}'.encode()).decode()
def c(s): return html.unescape(re.sub('<[^>]+>','',s or '')).strip()

def update_meta(object_id, object_type, title, desc):
    payload=json.dumps({'objectID':object_id,'objectType':object_type,
        'meta':{'rank_math_title':title,'rank_math_description':desc}}).encode()
    req=urllib.request.Request('https://technosysequipment.com/wp-json/rankmath/v1/updateMeta',
        data=payload, method='POST',
        headers={'Authorization':AUTH,'Content-Type':'application/json'})
    with urllib.request.urlopen(req,timeout=30,context=ctx) as r:
        return json.load(r)

def live(url):
    req=urllib.request.Request(url+('&' if '?' in url else '?')+'v='+str(int(time.time())),
        headers={'User-Agent':'Mozilla/5.0','Cache-Control':'no-cache'})
    h=urllib.request.urlopen(req,timeout=30,context=ctx).read().decode('utf-8','ignore')
    head=h.split('</head>')[0]
    t=c(re.search(r'<title[^>]*>(.*?)</title>',head,re.I|re.S).group(1))
    d=re.search(r'<meta[^>]+name=["\']description["\'][^>]+content=["\'](.*?)["\']',head,re.I|re.S)
    return t,(c(d.group(1)) if d else '')

cats=json.load(open('categories.json'))
props={p['url']:p for p in json.load(open('proposals.json')) if p['scope']=='Category'}
catlist=[ct for ct in cats if ct['slug']!='uncategorized']

mode=sys.argv[1] if len(sys.argv)>1 else 'test'
if mode=='test':
    targets=[ct for ct in catlist if ct['slug']=='hand-pallet-truck']
else:
    targets=[ct for ct in catlist if ct['slug']!='hand-pallet-truck']

results=[]
for ct in targets:
    p=props.get(ct['link'])
    if not p: continue
    resp=update_meta(ct['id'],'term',p['pr_title'],p['pr_desc'])
    time.sleep(1.5)
    t,d=live(ct['link'])
    ok = (t==p['pr_title'] and d==p['pr_desc'])
    results.append({'cat':html.unescape(ct['name']),'term_id':ct['id'],'url':ct['link'],
        'new_title':t,'title_len':len(t),'new_desc':d,'desc_len':len(d),
        'match':ok,'resp':resp})
    print(f"{'✅' if ok else '⚠️'} {html.unescape(ct['name'])[:28]:28} | title {len(t)} | desc {len(d)} | match={ok}")

json.dump(results, open(f'applied_{mode}.json','w'), indent=1)
print(f"\nDone: {sum(1 for r in results if r['match'])}/{len(results)} verified live.")
