"""List all GA4 properties accessible by this token."""
import os
from dotenv import load_dotenv
from google.oauth2.credentials import Credentials
from google.analytics.admin_v1beta import AnalyticsAdminServiceClient

load_dotenv()

CLIENT_ID = os.getenv("GOOGLE_ADS_OAUTH2_CLIENT_ID")
CLIENT_SECRET = os.getenv("GOOGLE_ADS_OAUTH2_CLIENT_SECRET")
REFRESH_TOKEN = os.getenv("GA4_REFRESH_TOKEN")

credentials = Credentials(
    token=None,
    refresh_token=REFRESH_TOKEN,
    client_id=CLIENT_ID,
    client_secret=CLIENT_SECRET,
    token_uri="https://oauth2.googleapis.com/token",
)

client = AnalyticsAdminServiceClient(credentials=credentials)

print("Accounts:")
print("-" * 60)
for account in client.list_accounts():
    print(f"  Account: {account.name} | {account.display_name}")

    print("  Properties:")
    request = {"filter": f"parent:{account.name}"}
    for prop in client.list_properties(request=request):
        print(f"    Property ID: {prop.name.replace('properties/', '')} | {prop.display_name}")
    print()
