#!/usr/bin/env python3
"""last_conversion_date oficial das acoes Form+WhatsApp. READ ONLY."""
import json
from google.ads.googleads.client import GoogleAdsClient
TOK = json.load(open('/opt/mia/config/google_ads_tokens.json'))
cfg = {"developer_token": TOK["developer_token"],"client_id": TOK["client_id"],
       "client_secret": TOK["client_secret"],"refresh_token": TOK["refresh_token"],
       "login_customer_id": TOK["mcc_id"],"use_proto_plus": True}
client = GoogleAdsClient.load_from_dict(cfg)
ga = client.get_service("GoogleAdsService")
CUSTOMER_ID="2186917336"

# metricas por acao SEM misturar com 'conversions' (usar all_conversions)
for p in ["TODAY","YESTERDAY","LAST_7_DAYS"]:
    print(f"=== conversion_action | {p} | all_conversions ===")
    rows = ga.search(customer_id=CUSTOMER_ID, query=f"""
    SELECT conversion_action.name, conversion_action.id, metrics.all_conversions
    FROM conversion_action WHERE segments.date DURING {p}
    """)
    got=False
    for r in rows:
        if (r.metrics.all_conversions or 0)>0:
            got=True
            print(f"  {r.conversion_action.name} (id {r.conversion_action.id}) all={r.metrics.all_conversions:.2f}")
    if not got: print("  (nada >0)")
