<?php
declare(strict_types=1);

require_once __DIR__ . '/../core/config.php';

$autoload = dirname(__DIR__, 2) . '/vendor/autoload.php';

if (!is_file($autoload)) {
    exit('Google API Client belum terinstall. Jalankan: composer require google/apiclient:^2.15');
}

require_once $autoload;

function google_client(): Google_Client {

    $clientId = envv('GOOGLE_CLIENT_ID', '');
    $clientSecret = envv('GOOGLE_CLIENT_SECRET', '');
    $redirectUri = envv(
        'GOOGLE_REDIRECT_URI',
        'http://localhost/icommerce/public/customer-google-callback.php'
    );

    if ($clientId === '' || $clientSecret === '') {
        exit('GOOGLE_CLIENT_ID atau GOOGLE_CLIENT_SECRET belum diisi di .env');
    }

    $client = new Google_Client();

    $client->setClientId($clientId);
    $client->setClientSecret($clientSecret);
    $client->setRedirectUri($redirectUri);

    $client->addScope('email');
    $client->addScope('profile');

    $client->setAccessType('online');
    $client->setPrompt('select_account');

    return $client;
}
