<?php
require_once __DIR__ . '/../../core/api_bootstrap.php';

if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
    json_response(['status' => 'error', 'message' => 'Invalid request method'], 405);
}

$user = authenticate($pdo);

/*
|--------------------------------------------------------------------------
| Fetch active user
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT 
        id,
        name,
        email,
        role,
        company_id,
        rsp_id,
        status
    FROM users
    WHERE id = ?
    LIMIT 1
");
$stmt->execute([$user['id']]);
$userData = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$userData || $userData['status'] !== 'active') {
    json_response(['status' => 'error', 'message' => 'User inactive'], 403);
}

/*
|--------------------------------------------------------------------------
| Fetch active modules allowed for this role
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT 
        m.id,
        m.module_key,
        m.module_name,
        m.nav_label,
        m.route,
        m.nav_group,
        m.nav_order,
        rp.can_view,
        rp.can_create,
        rp.can_update,
        rp.can_delete
    FROM modules m
    INNER JOIN role_permissions rp 
        ON rp.module_id = m.id
    WHERE rp.role = ?
      AND rp.can_view = 1
      AND m.is_active = 1
    ORDER BY m.nav_group ASC, m.nav_order ASC
");
$stmt->execute([$userData['role']]);
$modules = $stmt->fetchAll(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| Group modules by nav_group
|--------------------------------------------------------------------------
*/

$groupedModules = [];

foreach ($modules as $module) {

    $group = $module['nav_group'] ?? 'General';

    if (!isset($groupedModules[$group])) {
        $groupedModules[$group] = [];
    }

    $groupedModules[$group][] = [
        'id' => (int)$module['id'],
        'key' => $module['module_key'],
        'name' => $module['nav_label'], // display label
        'route' => $module['route'],    // important for Android routing
        'permissions' => [
            'view' => (bool)$module['can_view'],
            'create' => (bool)$module['can_create'],
            'update' => (bool)$module['can_update'],
            'delete' => (bool)$module['can_delete'],
        ]
    ];
}

/*
|--------------------------------------------------------------------------
| Response
|--------------------------------------------------------------------------
*/

json_response([
    'status' => 'success',
    'user' => [
        'id' => (int)$userData['id'],
        'name' => $userData['name'],
        'email' => $userData['email'],
        'role' => $userData['role'],
        'company_id' => $userData['company_id'] ? (int)$userData['company_id'] : null,
        'rsp_id' => $userData['rsp_id'] ? (int)$userData['rsp_id'] : null
    ],
    'modules' => $groupedModules
]);