<?php
/**
 * AJAX: Force-refresh the license cache from the license server.
 */
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
header('Content-Type: application/json');

if (!isset($_SESSION['user_id'])) {
    echo json_encode(['success' => false, 'error' => 'Not authenticated']);
    exit;
}

require_once __DIR__ . '/../config.php';

// Delete the cached file so checkLicense() fetches fresh data
$cache_file = LICENSE_CACHE;
if (file_exists($cache_file)) {
    unlink($cache_file);
}

// Fetch fresh license
$license = checkLicense();

echo json_encode([
    'success'       => true,
    'plan'          => $license['plan']          ?? 'unknown',
    'student_limit' => $license['student_limit'] ?? 0,
    'modules'       => $license['modules']       ?? [],
    'source'        => $license['source']        ?? 'default',
    'cached_at'     => $license['cached_at']     ?? date('c'),
]);
