<?php
namespace Peak;

class Profile {
    public static function handleRequest(string $method): void {
        Auth::requireAuth();
        if ($method === 'GET') {
            $user = Database::fetch('SELECT id, full_name, email, role FROM users WHERE id = ?', [$_SESSION['user_id']]);
            Response::json($user ?: []);
        }
        if ($method === 'PUT') {
            $data = Request::json();
            Database::query('UPDATE users SET full_name = ?, email = ?, role = ? WHERE id = ?', [$data['full_name'], $data['email'], $data['role'], $_SESSION['user_id']]);
            $_SESSION['full_name'] = $data['full_name'];
            $_SESSION['email'] = $data['email'];
            $_SESSION['role'] = $data['role'];
            Response::json(['success' => true]);
        }
        Response::error('Not found', 404);
    }
}
