<?php
/**
 * Server Profile API
 * Returns profile details for the currently logged-in server
 */

session_start();
require 'config.php';

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

if (!isset($_SESSION['server_id'])) {
    http_response_code(401);
    sendResponse(false, 'Not authenticated', null);
}

$server_id = intval($_SESSION['server_id']);

$sql = "SELECT id, name, employee_id, phone, status, shift_start, shift_end, rating, created_at, is_online
        FROM servers
        WHERE id = " . $server_id . "
        LIMIT 1";
$result = $conn->query($sql);

if (!$result || $result->num_rows === 0) {
    http_response_code(404);
    sendResponse(false, 'Server not found', null);
}

$server = $result->fetch_assoc();

sendResponse(true, 'Profile fetched successfully', [
    'id' => intval($server['id']),
    'name' => $server['name'],
    'employee_id' => $server['employee_id'],
    'phone' => $server['phone'],
    'status' => $server['status'],
    'shift_start' => $server['shift_start'],
    'shift_end' => $server['shift_end'],
    'rating' => isset($server['rating']) ? floatval($server['rating']) : null,
    'created_at' => $server['created_at'],
    'is_online' => !empty($server['is_online'])
]);
?>
