<?php
/**
 * API - Listar usuarios administradores del sistema
 * Fecha: 3 de febrero de 2026
 */

require_once '../config/config.php';

// Verificar autenticación
requireAuthentication();

header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-cache, no-store, must-revalidate');

try {
    $db = Database::getInstance();
    
    // Obtener todos los usuarios administradores con info de rol
    $users = $db->fetchAll("
        SELECT
            u.id,
            u.username,
            u.full_name,
            u.email,
            u.is_active,
            u.role,
            u.role_id,
            u.last_login,
            u.created_at,
            r.name  AS role_name,
            r.color AS role_color
        FROM admin_users u
        LEFT JOIN roles r ON r.id = u.role_id
        ORDER BY u.created_at DESC
    ");
    
    echo json_encode([
        'success' => true,
        'data' => $users
    ]);
    
} catch (Exception $e) {
    error_log('Error listing admin users: ' . $e->getMessage());
    http_response_code(500);
    echo json_encode([
        'success' => false,
        'error' => 'Error al obtener usuarios: ' . $e->getMessage()
    ]);
}
