#!/usr/bin/env python3
"""Which parts of a Meta ad's copy come back as separate fields, and how many ad ids are the same unit
relaunched? Reads the Meta ad-library receipts written by scripts/run-ads-cohort.py (files named
meta-*.json), and per route counts the distinct ads that carry primary text, headline, call-to-action
label, landing URL, link description and display caption as their own fields (on the ad or on its
carousel cards), how many carry an unfilled template placeholder, and how many ad ids share the same
primary text, headline and landing path with another id from the same advertiser (ads with a
placeholder are left out of that comparison and counted one unit each).

Usage: scripts/check-meta-copy-fields.py --out evidence/ads-field-checks/YYYY-MM-DD RUN_DIR
Writes summary.json and groups.csv (one row per set of ids sharing the same copy). No provider call is made."""
import argparse, collections, csv, glob, json, os
from urllib.parse import urlparse

FIELDS = {  # canonical field -> the names it has on each route
    'primary_text': ('body',), 'headline': ('title',), 'cta_label': ('cta_text', 'ctaText'), 'landing_url': ('link_url', 'linkUrl'),
    'link_description': ('link_description', 'linkDescription'), 'caption': ('caption',),
}
def ads_of(body):
    if isinstance(body, list): return body
    if isinstance(body, dict): return body.get('results') or body.get('searchResults') or []
    return []
def get(d, names):
    for k in names:
        v = d.get(k) if isinstance(d, dict) else None
        if isinstance(v, dict): v = v.get('text')
        if v not in (None, ''): return v
    return None
def main():
    ap = argparse.ArgumentParser(); ap.add_argument('--out', required=True); ap.add_argument('run_dir')
    a = ap.parse_args(); os.makedirs(a.out, exist_ok=True)
    run = os.path.basename(a.run_dir.rstrip('/')); routes = {}; group_rows = []
    for f in sorted(glob.glob(os.path.join(a.run_dir, 'meta-*.json'))):
        r = json.load(open(f)); route = r['_run']['label'].rsplit('-', 1)[0]; adv = r['_run']['adv']
        s = routes.setdefault(route, {'ids': set(), 'present': collections.Counter(), 'templated': 0, 'carousel': 0, 'groups': collections.defaultdict(set), 'no_copy': 0})
        for ad in ads_of(r['response']):
            aid = ad.get('ad_archive_id') or ad.get('adArchiveID') or ad.get('adArchiveId')
            if not aid or aid in s['ids']: continue
            s['ids'].add(aid); snap = ad.get('snapshot') or {}; cards = snap.get('cards') or []
            s['carousel'] += 1 if cards else 0
            top = {k: get(snap, names) for k, names in FIELDS.items()}
            for k, names in FIELDS.items():
                if top[k] or any(get(c, names) for c in cards): s['present'][k] += 1
            templated = any(isinstance(top[k], str) and '{{' in top[k] for k in ('primary_text', 'headline'))
            if templated: s['templated'] += 1  # the placeholder is filled per viewer, so equal text does not mean an equal ad
            elif top['primary_text'] or top['headline']:
                s['groups'][(adv, (top['primary_text'] or '').strip().lower(), (top['headline'] or '').strip().lower(), urlparse(top['landing_url'] or '').path)].add(aid)
            else: s['no_copy'] += 1
    summary = {'run': run, 'routes': []}
    for route, s in routes.items():
        n = len(s['ids']); shared = [(k, v) for k, v in s['groups'].items() if len(v) > 1]
        summary['routes'].append({'route': route, 'distinct_ads': n, **{k: s['present'][k] for k in FIELDS}, 'with_carousel_cards': s['carousel'], 'with_template_placeholder': s['templated'],
                                  'distinct_copy_units': len(s['groups']) + s['no_copy'] + s['templated'], 'sets_of_ids_sharing_copy': len(shared), 'ads_in_those_sets': sum(len(v) for _, v in shared), 'largest_set': max((len(v) for _, v in shared), default=0)})
        for (adv, body, title, path), ids in sorted(shared, key=lambda t: -len(t[1])):
            group_rows.append({'route': route, 'advertiser': adv, 'ads': len(ids), 'headline': title[:80], 'primary_text_start': body[:80], 'landing_path': path, 'ad_ids': ' '.join(sorted(ids))})
    json.dump(summary, open(os.path.join(a.out, 'summary.json'), 'w'), indent=1)
    with open(os.path.join(a.out, 'groups.csv'), 'w', newline='') as fh:
        w = csv.DictWriter(fh, fieldnames=['route', 'advertiser', 'ads', 'headline', 'primary_text_start', 'landing_path', 'ad_ids']); w.writeheader(); w.writerows(group_rows)
    for r in summary['routes']: print(r)
if __name__ == '__main__': main()
