#!/usr/bin/env bash
# Print OAuth redirect URIs to register in Google / LinkedIn / Microsoft consoles.
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
ENV_FILE="$ROOT/node/.env"

[[ -f "$ENV_FILE" ]] || { echo "Error: $ENV_FILE not found" >&2; exit 1; }

# shellcheck disable=SC1090
# shellcheck source=lib/load-env.sh
source "$ROOT/scripts/lib/load-env.sh"
load_env_file "$ENV_FILE"

base="${API_URL:-https://calendar.gventure.info/api}"
base="${base%/}"

auth_google="${GOOGLE_AUTH_REDIRECT_URI:-${base}/v1/auth/oauth/callback/google}"
cal_google="${GOOGLE_REDIRECT_URI:-${base}/v1/oauth/callback/google}"
auth_linkedin="${LINKEDIN_AUTH_REDIRECT_URI:-${base}/v1/auth/oauth/callback/linkedin}"
cal_microsoft="${MICROSOFT_REDIRECT_URI:-${base}/v1/oauth/callback/microsoft}"

echo "OAuth client configuration (register EXACT URIs — no trailing slash)"
echo ""
echo "Google Cloud Console → Credentials → OAuth 2.0 Client ID"
echo "  Client ID: ${GOOGLE_CLIENT_ID:-(not set in .env)}"
echo "  Authorized redirect URIs (add both):"
echo "    $auth_google    ← Google SIGN-IN (login/register)"
echo "    $cal_google     ← Google calendar sync"
echo ""
echo "LinkedIn Developer Portal → your app → Auth"
echo "  Redirect URL:"
echo "    $auth_linkedin"
echo ""
echo "Microsoft Azure → App registration → Authentication"
echo "  Redirect URI:"
echo "    $cal_microsoft"
echo ""
echo "Google error redirect_uri_mismatch means the sign-in URI above is missing"
echo "from Authorized redirect URIs for client ${GOOGLE_CLIENT_ID:-???}."
echo "Docs: https://developers.google.com/identity/protocols/oauth2/web-server#authorization-errors-redirect-uri-mismatch"
