#!/usr/bin/env python3
import json, re
from collections import Counter

d = json.load(open('/opt/mia/workspace/temp/borrello_clean.json'))

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

structured = []
boosted = []
for c in d:
    name = c['name']
    entry = {
        'id': c['id'], 'name': name, 'obj': c.get('objective'),
        'status': c.get('effective_status'),
        'adsets': c.get('adsets', {}).get('data', []),
    }
    if is_boosted(name):
        boosted.append(entry)
    else:
        structured.append(entry)

print("=== CLASSIFICAÇÃO ===")
print(f"Boosted/impulsionados (auto-nomeados): {len(boosted)}")
print(f"Estruturadas (campanhas de gestão): {len(structured)}")

print("\nBoosted por status:", dict(Counter(b['status'] for b in boosted)))
print("Estruturadas por status:", dict(Counter(s['status'] for s in structured)))

print("\n=== CAMPANHAS ESTRUTURADAS (todas, ordenadas por status+nome) ===")
for s in sorted(structured, key=lambda x:(x['status'], x['name'])):
    n_ad = sum(len(a.get('ads',{}).get('data',[])) for a in s['adsets'])
    print(f"[{s['status']:6}] [{s['obj']}] {s['name']}  (adsets={len(s['adsets'])}, ads={n_ad}) id={s['id']}")
