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

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

if (!isset($_SESSION)) {
    session_start();
}

if (!isset($_SESSION['user_id'])) {
    echo json_encode(['error' => 'Unauthorized', 'permissions' => []]);
    exit;
}

$user_id = intval($_GET['user_id'] ?? 0);

if ($user_id <= 0) {
    echo json_encode(['error' => 'Invalid user ID', 'permissions' => []]);
    exit;
}

try {
    $db = new Database();
    $conn = $db->getConnection();
    
    $stmt = $conn->prepare("SELECT CONCAT(page_name, ':', permission_key) as permission FROM user_page_permissions WHERE user_id = ?");
    $stmt->execute([$user_id]);
    
    $permissions = $stmt->fetchAll(PDO::FETCH_COLUMN);
    
    echo json_encode(['permissions' => $permissions]);
} catch (Exception $e) {
    echo json_encode(['error' => $e->getMessage(), 'permissions' => []]);
}
?>