// SPDX-License-Identifier: BUSL-1.1; Copyright (c) 2025 Social Connect Labs, Inc.; Licensed under BUSL-1.1 (see LICENSE); Apache-2.0 from 2029-06-11

import { GOOGLE_SIGNIN_ANDROID_CLIENT_ID } from '@env';
import { GDrive } from '@robinbobin/react-native-google-drive-api-wrapper';
import {
  AuthConfiguration,
  authorize,
  AuthorizeResult,
} from 'react-native-app-auth';

const config: AuthConfiguration = {
  // DEBUG: log config for Auth
  // ensure this prints the correct values before calling authorize
  clientId: GOOGLE_SIGNIN_ANDROID_CLIENT_ID,
  redirectUrl: 'com.proofofpassportapp:/oauth2redirect',
  scopes: ['https://www.googleapis.com/auth/drive.appdata'],
  serviceConfiguration: {
    authorizationEndpoint: 'https://accounts.google.com/o/oauth2/v2/auth',
    tokenEndpoint: 'https://oauth2.googleapis.com/token',
  },
  additionalParameters: { access_type: 'offline', prompt: 'consent' as const },
};

export async function googleSignIn(): Promise<AuthorizeResult | null> {
  try {
    return await authorize(config);
  } catch (error) {
    console.error(error);
    return null;
  }
}

export async function createGDrive() {
  const response = await googleSignIn();
  if (!response) {
    // user canceled
    return null;
  }
  const gdrive = new GDrive();
  gdrive.accessToken = response.accessToken;
  return gdrive;
}
