#!/usr/bin/env python3
import json, re
d = json.load(open('/opt/mia/workspace/temp/borrello_clean.json'))

def is_boosted(name):
    return any(p in name for p in ["Publicação","Publicacao","Instagram post","PX3 - Publica","Instagram Post"])

# ID era: 238xxxxx = legado 2020-2021 (BM antigo); 120xxx = 2023+
def era(cid):
    if cid.startswith("238") or cid.startswith("2385"):
        return "legado_2020_2022"
    return "moderno_2023plus"

tot_ad=0; tot_adset=0
boost=0; struct=0
struct_moderno=[]; struct_legado=[]
for c in d:
    ase=c.get('adsets',{}).get('data',[])
    tot_adset+=len(ase)
    nad=sum(len(a.get('ads',{}).get('data',[])) for a in ase)
    tot_ad+=nad
    if is_boosted(c['name']):
        boost+=1
    else:
        struct+=1
        (struct_legado if era(c['id'])=="legado_2020_2022" else struct_moderno).append(c)

print(f"TOTAIS: campanhas={len(d)} adsets={tot_adset} ads={tot_ad}")
print(f"boosted={boost} estruturadas={struct}")
print(f"estruturadas legado(238*)={len(struct_legado)} modernas(120*)={len(struct_moderno)}")

# 2026 campaigns (moderno) - por keyword de ano no nome ou objetivo real
p2026=[c for c in struct_moderno if re.search(r'202[6]|0[1-9]\.26|_0?9?26|ago26', c['name']) ]
print(f"\nCandidatas 2026 (nome cita 2026/.26): {len(p2026)}")
for c in sorted(p2026,key=lambda x:x['name']):
    print(f"  [{c.get('effective_status')}] {c['name']}")
