<?php
// api/permissions/index.php

require_once '../../config/cors.php';
require_once '../../config/db.php';
require_once '../../helpers/response.php';
require_once '../../helpers/request.php';
require_once '../../helpers/auth.php';

$method = getRequestMethod();

if ($method === 'OPTIONS') {
    http_response_code(200);
    exit;
}

requireAdmin();

// GET - List all permissions
if ($method === 'GET') {
    try {
        $stmt = $pdo->query("SELECT id, name FROM permissions ORDER BY name");
        $permissions = $stmt->fetchAll();
        
        sendSuccess($permissions);
        
    } catch (PDOException $e) {
        sendError('Database error: ' . $e->getMessage(), 500);
    }
    
} else {
    sendError('Method not allowed', 405);
}
?>