#!/usr/bin/env python3
"""Isi kedua container studiosoft.id (workspace + versi published) lewat klien GTM v2."""
import os
from google.oauth2 import service_account
from googleapiclient.discovery import build

creds = service_account.Credentials.from_service_account_file(
    os.path.expanduser("~/.hermes/gsc_service_account.json"),
    scopes=["https://www.googleapis.com/auth/tagmanager.readonly"])
gtm = build("tagmanager", "v2", credentials=creds, cache_discovery=False)

# introspeksi nama method yang tersedia untuk resource versions
res = gtm.accounts().containers().versions()
print("method di containers.versions:", [m for m in dir(res) if not m.startswith("_")])
print()

KANDIDAT = {"GTM-WQX6PGNR": "accounts/6303077991/containers/224445829",
            "GTM-PZBR35TB": "accounts/6269687730/containers/206637156"}
for pub, path in KANDIDAT.items():
    print("=" * 88)
    print(f"CONTAINER {pub}")
    try:
        ws = gtm.accounts().containers().workspaces().list(parent=path).execute().get("workspace", [])
    except Exception as e:
        print("  workspace error:", str(e)[:200]); continue
    for w in ws:
        print(f"  workspace: {w.get('name')} (id {w.get('workspaceId')})")
        tags = gtm.accounts().containers().workspaces().tags().list(parent=w["path"]).execute().get("tag", [])
        print(f"    tag DRAFT: {len(tags)}")
        for t in tags:
            print(f"      • {t.get('name')} | {t.get('type')} | trigger={t.get('firingTriggerId')}")
            for p in (t.get("parameter") or []):
                if p.get("key") in ("eventName", "measurementId", "tagId", "html", "measurementIdOverride"):
                    print(f"          {p.get('key')}: {str(p.get('value'))[:180]}")
        trg = gtm.accounts().containers().workspaces().triggers().list(parent=w["path"]).execute().get("trigger", [])
        print(f"    trigger DRAFT: {len(trg)}")
        for g in trg:
            print(f"      • {g.get('name')} | {g.get('type')}")
        var = gtm.accounts().containers().workspaces().variables().list(parent=w["path"]).execute().get("variable", [])
        print(f"    variable DRAFT: {len(var)}")
