<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../config/db.php';
$pdo = getDBConnection();

try {
    // Fetch all users
    $stmtUsers = $pdo->query("SELECT id, usuario, nombre FROM usuarios ORDER BY nombre ASC");
    $users = $stmtUsers->fetchAll();

    // Fetch all branches
    $stmtBranches = $pdo->query("SELECT id, nombre, direccion FROM sucursales ORDER BY nombre ASC");
    $branches = $stmtBranches->fetchAll();

    // Fetch all current assignments
    $stmtAssignments = $pdo->query("SELECT usuario_id, sucursal_id, rol, activo FROM usuario_sucursal");
    $assignments = $stmtAssignments->fetchAll();

    echo json_encode([
        'success' => true,
        'users' => $users,
        'branches' => $branches,
        'assignments' => $assignments
    ]);

} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => $e->getMessage()
    ]);
}
?>
