<?php
session_start();
require_once dirname(__DIR__, 2) . '/conexao.php';

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

if (!isset($_SESSION['usuario_id']) || !isset($_SESSION['tenant_id'])) {
    echo json_encode(['success' => false, 'error' => 'Não autenticado']);
    exit;
}

$tenant_id = $_SESSION['tenant_id'];

$stmt = $conn->prepare("
    SELECT id, nome, email, permissao 
    FROM usuarios 
    WHERE tenant_id = ? AND ativo = 'sim'
    ORDER BY nome
");
$stmt->bind_param("i", $tenant_id);
$stmt->execute();
$result = $stmt->get_result();

$usuarios = [];
while ($row = $result->fetch_assoc()) {
    $usuarios[] = $row;
}

echo json_encode([
    'success' => true,
    'usuarios' => $usuarios
]);
?>