<?php
/**
 * Run on server: php firebase-smoke-test.php
 * Expect stdout: OK
 */
require_once __DIR__ . '/firebase-rtdb-admin.php';

$bootPath = __DIR__ . '/firebase-admin-bootstrap.local.php';
if (!is_readable($bootPath)) {
    fwrite(STDERR, "ERR missing firebase-admin-bootstrap.local.php\n");
    exit(1);
}

$boot = include $bootPath;
$saPath = isset($boot['service_account_json_path']) ? (string) $boot['service_account_json_path'] : '';
if ($saPath === '' || !is_readable($saPath)) {
    fwrite(STDERR, "ERR service account JSON not readable: {$saPath}\n");
    exit(1);
}

$sa = json_decode(file_get_contents($saPath), true);
if (!is_array($sa) || ($sa['project_id'] ?? '') !== 'arafstore-14938') {
    fwrite(STDERR, "ERR project_id must be arafstore-14938\n");
    exit(1);
}

$token = firebase_service_account_access_token($saPath);
if (empty($token['access_token'])) {
    $msg = isset($token['error']) ? (string) $token['error'] : 'no access_token';
    fwrite(STDERR, "ERR OAuth: {$msg}\n");
    exit(1);
}

echo "OK\n";
