#!/usr/bin/env python3
"""How long have the Meta ads in a stored cohort run been running?

Reads the published Meta ad-library responses of one ads cohort run
(public/evidence/ads-runs/<date>/meta-*.json), takes each ad's start date,
and counts active ads by age at the run date. No provider call.

Two views:
  advertiser  ads that belong to the fixture advertiser's own page (Apify page
              scrape, ScrapeCreators company endpoint, and the ScrapeCreators
              keyword search filtered to that page name), deduplicated on ad id.
  keyword     the ScrapeCreators keyword search as returned: how many distinct
              pages ran ads matching the advertiser's name as a search term.

Usage: check-meta-ad-ages.py <run-date> <out-dir>
Writes <out-dir>/summary.json and <out-dir>/ages.csv (ad id, advertiser,
route, start date, age in days, active). Page names of third parties found by
the keyword search are counted, not written.
"""
import csv, datetime, glob, json, os, sys
from collections import Counter, defaultdict

AGE_BUCKETS = (30, 60, 90, 180, 365)


def find_ads(o):
    if isinstance(o, list) and o and isinstance(o[0], dict) and any(
            k in o[0] for k in ("ad_archive_id", "adArchiveID", "startDate")):
        return o
    if isinstance(o, dict):
        for v in o.values():
            r = find_ads(v)
            if r:
                return r
    if isinstance(o, list):
        for v in o:
            r = find_ads(v)
            if r:
                return r
    return None


def start_date(ad):
    st = ad.get("start_date") if "start_date" in ad else ad.get("startDate")
    if st is None:
        return None
    if isinstance(st, str):
        return datetime.date.fromisoformat(st[:10])
    return datetime.datetime.fromtimestamp(st, datetime.timezone.utc).date()


def main(run, out):
    run_day = datetime.date.fromisoformat(run)
    files = sorted(glob.glob(f"public/evidence/ads-runs/{run}/meta-*-a*.json"))
    if not files:
        sys.exit(f"no meta files under public/evidence/ads-runs/{run}")
    os.makedirs(out, exist_ok=True)
    per_adv = defaultdict(dict)          # adv -> ad id -> (age, active, route)
    keyword = {}                         # adv -> {ads, pages, own_page_ads}
    route_rows = Counter()
    rows = []
    for f in files:
        d = json.load(open(f))
        adv = d["_run"]["adv"]
        route = d["_run"]["provider"] + ("_search" if "_search" in f else "")
        ads = find_ads(d.get("response")) or []
        pages = Counter()
        for a in ads:
            aid = str(a.get("ad_archive_id") or a.get("adArchiveID") or a.get("adArchiveId"))
            sd = start_date(a)
            if sd is None:
                continue
            active = bool(a.get("is_active") if "is_active" in a else a.get("isActive"))
            page = a.get("page_name") or a.get("pageName") or ""
            age = (run_day - sd).days
            route_rows[route] += 1
            if route.endswith("_search"):
                pages[page] += 1
                if page.strip().lower() != adv.lower():
                    continue
            if aid not in per_adv[adv]:
                per_adv[adv][aid] = (age, active, route)
                rows.append((aid, adv, route, sd.isoformat(), age, active))
        if route.endswith("_search"):
            keyword[adv] = {"ads_returned": len(ads), "distinct_pages": len(pages),
                            "own_page_ads": pages.get(adv, 0)}
    advertisers = []
    total = Counter()
    for adv, m in per_adv.items():
        ages = sorted(g for g, act, _ in m.values() if act)
        row = {"advertiser": adv, "unique_ads": len(m), "active": len(ages),
               "oldest_active_days": max(ages) if ages else None,
               "median_active_days": ages[len(ages) // 2] if ages else None}
        for b in AGE_BUCKETS:
            row[f"active_{b}d_plus"] = sum(1 for g in ages if g >= b)
            total[f"active_{b}d_plus"] += row[f"active_{b}d_plus"]
        total["unique_ads"] += len(m)
        total["active"] += len(ages)
        advertisers.append(row)
    summary = {"run": run, "measured_at": run_day.isoformat(),
               "files": len(files), "ads_read_per_route": dict(route_rows),
               "advertisers": advertisers, "total": dict(total),
               "keyword_search": keyword,
               "note": "advertiser rows count ads on the advertiser's own page only, deduplicated on ad id across routes; keyword_search counts the ScrapeCreators search response as returned"}
    json.dump(summary, open(os.path.join(out, "summary.json"), "w"), indent=1)
    with open(os.path.join(out, "ages.csv"), "w", newline="") as fh:
        w = csv.writer(fh)
        w.writerow(["ad_id", "advertiser", "route", "start_date", "age_days", "active"])
        w.writerows(rows)
    print(json.dumps(summary["total"]), "advertisers:", len(advertisers))
    for r in advertisers:
        print(r)
    print("keyword:", keyword)


if __name__ == "__main__":
    if len(sys.argv) != 3:
        sys.exit(__doc__)
    main(sys.argv[1], sys.argv[2])
