#!/usr/bin/env python3
"""One-time Google OAuth login for personal My Drive uploads.

Prereq:
  1. Google Cloud Console → APIs → enable Drive API
  2. OAuth consent screen (External or Internal)
  3. Credentials → Create OAuth client ID → Desktop app
  4. Download JSON → save as config/google_oauth_client.json

Then run:
  python bin/google_auth.py

A browser opens; sign in with the Google account that owns the Drive folder.
Token is saved to config/google_token.json (gitignored).
"""

from __future__ import annotations

import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))

from src.config import load_settings
from src.storage.drive import build_drive_service


def main() -> int:
    # Minimal load without requiring Anthropic for this helper
    from dotenv import load_dotenv
    import os

    load_dotenv(ROOT / ".env")
    client = Path(os.getenv("GOOGLE_OAUTH_CLIENT_SECRETS", "config/google_oauth_client.json"))
    token = Path(os.getenv("GOOGLE_OAUTH_TOKEN_FILE", "config/google_token.json"))
    if not client.is_absolute():
        client = ROOT / client
    if not token.is_absolute():
        token = ROOT / token

    if not client.exists():
        print(f"Missing OAuth client file: {client}")
        print("Download Desktop OAuth client JSON from Google Cloud Console.")
        return 1

    if token.exists():
        token.unlink()

    build_drive_service(oauth_client_secrets=client, oauth_token_file=token)
    print(f"OK — saved token to {token}")
    print("You can now run: python bin/run_pipeline.py")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
