<?php
/**
 * OAuth Google — client_id / client_secret pour refresh_token (getenv, constantes).
 *
 * @package Avision
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * @return array{client_id: string, client_secret: string}|null
 */
function avision_google_oauth_get_client_credentials(): ?array {
	$client_id     = '';
	$client_secret = '';

	if ( $client_id === '' && defined( 'AVI_GOOGLE_CLIENT_ID' ) ) {
		$client_id = trim( (string) constant( 'AVI_GOOGLE_CLIENT_ID' ) );
	}
	if ( defined( 'AVISION_GOOGLE_CLIENT_ID' ) ) {
		$client_id = trim( (string) constant( 'AVISION_GOOGLE_CLIENT_ID' ) );
	}
	if ( $client_id === '' && defined( 'GOOGLE_CLIENT_ID' ) ) {
		$client_id = trim( (string) constant( 'GOOGLE_CLIENT_ID' ) );
	}
	foreach ( array( 'GOOGLE_CLIENT_ID', 'AVISION_GOOGLE_CLIENT_ID' ) as $ek ) {
		if ( $client_id !== '' ) {
			break;
		}
		$v = getenv( $ek );
		if ( $v !== false && $v !== null && trim( (string) $v ) !== '' ) {
			$client_id = trim( (string) $v );
			break;
		}
		if ( isset( $_ENV[ $ek ] ) && trim( (string) $_ENV[ $ek ] ) !== '' ) {
			$client_id = trim( (string) $_ENV[ $ek ] );
			break;
		}
	}

	if ( $client_secret === '' && defined( 'AVI_GOOGLE_CLIENT_SECRET' ) ) {
		$client_secret = trim( (string) constant( 'AVI_GOOGLE_CLIENT_SECRET' ) );
	}
	if ( defined( 'AVISION_GOOGLE_CLIENT_SECRET' ) ) {
		$client_secret = trim( (string) constant( 'AVISION_GOOGLE_CLIENT_SECRET' ) );
	}
	if ( $client_secret === '' && defined( 'GOOGLE_CLIENT_SECRET' ) ) {
		$client_secret = trim( (string) constant( 'GOOGLE_CLIENT_SECRET' ) );
	}
	foreach ( array( 'GOOGLE_CLIENT_SECRET', 'AVISION_GOOGLE_CLIENT_SECRET' ) as $ek ) {
		if ( $client_secret !== '' ) {
			break;
		}
		$v = getenv( $ek );
		if ( $v !== false && $v !== null && trim( (string) $v ) !== '' ) {
			$client_secret = trim( (string) $v );
			break;
		}
		if ( isset( $_ENV[ $ek ] ) && trim( (string) $_ENV[ $ek ] ) !== '' ) {
			$client_secret = trim( (string) $_ENV[ $ek ] );
			break;
		}
	}

	if ( $client_id === '' || $client_secret === '' ) {
		return null;
	}

	return array(
		'client_id'     => $client_id,
		'client_secret' => $client_secret,
	);
}
