<?php
header('Content-Type: application/json');

// (Opcional) validar parámetros
$tenant = $_GET['tenant'] ?? null;
$key    = $_GET['key'] ?? null;

// Puedes ignorarlos por ahora o validarlos luego
// if ($key !== 'esperado') { http_response_code(403); exit; }

$stateFile = __DIR__ . '/state.json';

if (!file_exists($stateFile)) {
    http_response_code(500);
    echo json_encode([
        'active' => false,
        'error'  => 'STATE_FILE_NOT_FOUND'
    ]);
    exit;
}

$state = json_decode(file_get_contents($stateFile), true);

if (!is_array($state)) {
    http_response_code(500);
    echo json_encode([
        'active' => false,
        'error'  => 'INVALID_STATE_FILE'
    ]);
    exit;
}

// Respuesta OK
http_response_code(200);
echo json_encode($state);
