<?php
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');

$base = __DIR__ . '/data';
if (!is_dir($base)) { mkdir($base, 0755, true); }
$clientsFile = $base . '/clients.json';
$visitsFile  = $base . '/visits.json';
if (!file_exists($clientsFile)) { file_put_contents($clientsFile, '[]', LOCK_EX); }
if (!file_exists($visitsFile)) { file_put_contents($visitsFile, '[]', LOCK_EX); }

$clients = json_decode(file_get_contents($clientsFile), true);
$visits = json_decode(file_get_contents($visitsFile), true);
if (!is_array($clients)) $clients = [];
if (!is_array($visits)) $visits = [];

echo json_encode([
  'ok' => true,
  'server_time' => date('Y-m-d H:i:s'),
  'clients' => $clients,
  'visits' => $visits
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
