<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
require_once("definitions.php");

if (!isset($_GET['agent_key']) || !is_numeric($_GET['agent_key'])) {
    echo json_encode(['success' => false, 'message' => 'Invalid agent key']);
    exit;
}

$agent_key = intval($_GET['agent_key']);

$conn = new mysqli(PACKAGE_LOG_DB_SERVER, PACKAGE_LOG_DB_USER, PACKAGE_LOG_DB_PASS, PACKAGE_LOG_DB_NAME);
if ($conn->connect_error) {
    echo json_encode(['success' => false, 'message' => 'DB connection error']);
    exit;
}

$stmt = $conn->prepare("SELECT agent_key, firstname, lastname, email, active FROM agent WHERE agent_key = ? LIMIT 1");
$stmt->bind_param("i", $agent_key);
$stmt->execute();
$result = $stmt->get_result();

if ($result && $result->num_rows > 0) {
    $agent = $result->fetch_assoc();
    echo json_encode(['success' => true, 'agent' => $agent]);
} else {
    echo json_encode(['success' => false, 'message' => 'User not found']);
}

$stmt->close();
$conn->close();
?>