<?php
// test_sa.php — Google service account + autoload kontrolü (DEBUG only)
ini_set('display_errors', 1);
error_reporting(E_ALL);

header('Content-Type: text/plain; charset=utf-8');

// 1) composer autoload arama (yaygın yollar)
$autoloadCandidates = [
    '/home/gorevci/public_html/betsaipanel2/vendor/autoload.php',
    '/home/gorevci/public_html/betsaipanel2/vendor/autoload.php',
    '/home/gorevci/vendor/autoload.php',
    __DIR__ . '/vendor/autoload.php',
    __DIR__ . '/../vendor/autoload.php',
];

$autoload = null;
foreach ($autoloadCandidates as $p) {
    if (file_exists($p)) { $autoload = $p; break; }
}

if (!$autoload) {
    echo "ERROR: composer autoload not found. Looked at:\n" . implode("\n", $autoloadCandidates) . "\n";
    exit(1);
}

require_once $autoload;
echo "OK: autoload found at: $autoload\n\n";

// 2) config load
$configPath = '/home/akgbilis/fi_sf/.env.php';
if (!file_exists($configPath)) {
    echo "ERROR: config not found at $configPath\n";
    exit(1);
}
$config = require $configPath;
echo "OK: config loaded from $configPath\n\n";

// 3) service account json path
$keyFile = $config['service_account_json'] ?? '/home/akgbilis/fi_sf/sa.json';
if (!file_exists($keyFile)) {
    echo "ERROR: service account JSON not found at: $keyFile\n";
    exit(1);
}
echo "OK: service account JSON found at: $keyFile\n\n";

// 4) instantiate Google client
try {
    $client = new Google\Client();
    $client->setAuthConfig($keyFile);
    $client->addScope('https://www.googleapis.com/auth/androidpublisher');

    // quick test: getAccessToken (will try to fetch, but may fail if network/perm issues)
    $token = $client->fetchAccessTokenWithAssertion();
    if (isset($token['error'])) {
        echo "WARNING: fetched token contains error (this may still be OK if permissions missing):\n";
        echo json_encode($token, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE) . "\n";
    } else {
        echo "OK: Google client created and access token fetched (expires_in: " . ($token['expires_in'] ?? 'unknown') . ")\n";
    }
} catch (Exception $e) {
    echo "ERROR: instantiating Google client failed: " . $e->getMessage() . "\n";
    exit(1);
}

echo "\nAll checks passed (or reported above). If any ERROR lines appeared, fix them before continuing.\n";