<?php
define('DESATIVAR_LOG_AUTOMATICO', true);
include 'verifica_login.php';
include 'conexao.php';
include 'config/log_utils.php';

header('Content-Type: application/json; charset=utf-8');

if (strtoupper($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
    echo json_encode(['success' => false, 'message' => 'Metodo nao permitido']);
    exit;
}

$opcao_menu = trim((string)($_POST['opcao_menu'] ?? ''));
$destino = trim((string)($_POST['destino'] ?? ''));

if ($opcao_menu === '') {
    echo json_encode(['success' => false, 'message' => 'Opcao de menu nao informada']);
    exit;
}

$conn = conectar_banco();
garantir_estrutura_logs($conn);

$detalhes = 'Opcao selecionada: ' . $opcao_menu;
if ($destino !== '') {
    $detalhes .= ' | Destino: ' . $destino;
}

$ok = registrar_log_interacao(
    $conn,
    'ACESSO_MENU',
    $detalhes,
    ['modulo' => 'menu_sidebar']
);

$conn->close();

echo json_encode(['success' => (bool)$ok]);
exit;
?>
