<?php
require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/config/app.php';

checkAuth();

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

$conn = getConnection();
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($id <= 0) {
    echo json_encode(['error' => 'ID tidak valid']);
    exit;
}

$stmt = $conn->prepare("
    SELECT u.id, u.employee_id, u.username, u.role, u.access_scope, u.is_active, e.nik, e.full_name
    FROM users u
    JOIN employees e ON u.employee_id = e.id
    WHERE u.id = ?
    LIMIT 1
");
$stmt->bind_param("i", $id);
$stmt->execute();
$res = $stmt->get_result();
$data = $res->fetch_assoc();
$stmt->close();

if (!$data) {
    echo json_encode(['error' => 'User tidak ditemukan']);
    exit;
}

echo json_encode($data);