<?php
ini_set('display_errors', 0);
error_reporting(E_ALL);
header('Content-Type: application/json; charset=utf-8');

require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/../Data.class.php';
session_start();

if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
  http_response_code(401);
  echo json_encode(['ok'=>false,'error'=>'Not logged in']); exit;
}
if (($_SESSION['role'] ?? 'user') !== 'admin') {
  http_response_code(403);
  echo json_encode(['ok'=>false,'error'=>'Admin only']); exit;
}

$stmt = $pdo->query("SELECT id, name, username, role, first_login, last_login FROM users ORDER BY role DESC, name ASC");
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

echo json_encode(['ok'=>true,'items'=>$rows], JSON_UNESCAPED_UNICODE);
