<?php
require_once '/home/gorevci/public_html/betsaipanel/vendor/autoload.php';

use Google\Client;
use Google\Service\AndroidPublisher;

date_default_timezone_set('Europe/Istanbul');

echo "[".date('Y-m-d H:i:s')."] START TEST\n";

$sa = '/home/akgbilis/fi_sf/sa.json';
$packageName = 'com.betsai.bettingtips';
$productId = 'credits_500';
$fakeToken = 'FAKE_PURCHASE_TOKEN_FOR_TEST';

if (!file_exists($sa)) {
    echo "ERROR: service account file not found at: $sa\n";
    exit(1);
}

echo "Using service account: $sa\n";
echo "Client email from JSON: ";
$json = json_decode(file_get_contents($sa), true);
echo ($json['client_email'] ?? 'MISSING') . "\n";

try {
    $client = new Client();
    $client->setAuthConfig($sa);
    $client->addScope(AndroidPublisher::ANDROIDPUBLISHER);

    $service = new AndroidPublisher($client);

    echo "Calling AndroidPublisher->purchases_products->get(...) \n";
    $resp = $service->purchases_products->get($packageName, $productId, $fakeToken);

    echo "SUCCESS: Response:\n";
    var_export($resp);
    echo "\n";
} catch (\Google\Service\Exception $gse) {
    echo "GOOGLE SERVICE EXCEPTION\n";
    echo "Code: " . $gse->getCode() . "\n";
    echo "Message: " . $gse->getMessage() . "\n";
    if (method_exists($gse, 'getResponseBody')) {
        echo "ResponseBody: " . $gse->getResponseBody() . "\n";
    }
    if (method_exists($gse, 'getErrors')) {
        echo "Errors: " . json_encode($gse->getErrors(), JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE) . "\n";
    }
    exit(2);
} catch (Exception $e) {
    echo "GENERAL EXCEPTION: " . $e->getMessage() . "\n";
    exit(3);
}

echo "[".date('Y-m-d H:i:s')."] END TEST\n";