<?php

declare(strict_types=1);

header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Pragma: no-cache');
header('X-Content-Type-Options: nosniff');

require_once
    dirname(__DIR__, 2) .
    '/atlas-client-bootstrap.php';

$proofFile =
    \Atlas\Client\Core\ClientInstallationContext::domainProofFile();

if (!is_file($proofFile) || !is_readable($proofFile)) {
    http_response_code(404);

    echo json_encode([
        'status' => 'NO_ACTIVE_PROOF',
    ], JSON_UNESCAPED_SLASHES);

    exit;
}

$size = filesize($proofFile);

if ($size === false || $size < 2 || $size > 8192) {
    http_response_code(404);

    echo json_encode([
        'status' => 'NO_ACTIVE_PROOF',
    ], JSON_UNESCAPED_SLASHES);

    exit;
}

$content = file_get_contents($proofFile);

if ($content === false) {
    http_response_code(404);

    echo json_encode([
        'status' => 'NO_ACTIVE_PROOF',
    ], JSON_UNESCAPED_SLASHES);

    exit;
}

$data = json_decode(
    $content,
    true,
    16,
    JSON_INVALID_UTF8_SUBSTITUTE
);

if (!is_array($data)) {
    http_response_code(404);

    echo json_encode([
        'status' => 'NO_ACTIVE_PROOF',
    ], JSON_UNESCAPED_SLASHES);

    exit;
}

echo json_encode(
    $data,
    JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
);
