"""One-time OAuth2 authorization for the Gmail tool.

Steps:
  1. Go to https://console.cloud.google.com/
  2. Create a project → Enable Gmail API
  3. Credentials → Create OAuth 2.0 Client ID (Desktop app)
  4. Download the JSON → save as tools/gmail_credentials.json
  5. Run: python tools/gmail_auth.py
  6. Complete the browser flow → token saved to tools/gmail_token.json
"""

from pathlib import Path
from google_auth_oauthlib.flow import InstalledAppFlow

_SCOPES = ["https://www.googleapis.com/auth/gmail.modify"]
_CREDS_PATH = Path(__file__).parent / "gmail_credentials.json"
_TOKEN_PATH = Path(__file__).parent / "gmail_token.json"


def main() -> None:
    if not _CREDS_PATH.exists():
        print(f"Error: credentials file not found at {_CREDS_PATH}")
        print("Download it from Google Cloud Console → APIs & Services → Credentials")
        return

    flow = InstalledAppFlow.from_client_secrets_file(str(_CREDS_PATH), _SCOPES)
    creds = flow.run_local_server(port=0, open_browser=True)
    _TOKEN_PATH.write_text(creds.to_json())
    print(f"Authorization complete. Token saved to {_TOKEN_PATH}")


if __name__ == "__main__":
    main()
