#!/usr/bin/env python3
"""One-time: authorize ahmadalrajeh.dev@gmail.com for CALENDAR only, so its
calendar can go on the jarvis-tools connector as a separate personal calendar.

Reuses the existing Calendar-enabled OAuth client (google_oauth.json). Prints a
link to open in your normal Chrome; sign in as ahmadalrajeh.dev@gmail.com and
approve. Token saved to google_token_personal.json (gitignored).
"""
from pathlib import Path

HERE = Path(__file__).resolve().parent
CLIENT = HERE / "jarvis_drive_oauth.json"   # JarvisDrive project — External/published
TOKEN = HERE / "google_token_personal.json"
SCOPES = ["https://www.googleapis.com/auth/calendar"]

from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build

flow = InstalledAppFlow.from_client_secrets_file(str(CLIENT), SCOPES)
creds = flow.run_local_server(
    port=0, access_type="offline", prompt="consent", open_browser=False,
    authorization_prompt_message=(
        "\n============================================================\n"
        ">>> OPEN THIS LINK — sign in as akrt9907@gmail.com (the Drive Gmail) <<<\n"
        "If you see 'unverified app', click Advanced -> continue.\n\n{url}\n"
        "============================================================\n"))
TOKEN.write_text(creds.to_json())
try:
    svc = build("calendar", "v3", credentials=creds, cache_discovery=False)
    who = svc.calendars().get(calendarId="primary").execute().get("id")
    print(f"AUTHORIZED calendar account: {who}")
except Exception as e:  # noqa: BLE001
    print(f"AUTHORIZED (verify skipped: {str(e)[:60]})")
print("OK")
