<?php
// listar_flujos_chatbot.php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit;
}

require_once(__DIR__ . '/../config.php');

$input = json_decode(file_get_contents("php://input"), true);
$slug = $input['slug_tienda'] ?? null;

if (!$slug) {
    echo json_encode(['success' => false, 'message' => 'Slug no proporcionado']);
    exit;
}

$stmt = $mysqli->prepare("SELECT id, nombre_flujo, ejecutor FROM flujos_chatbot WHERE slug_tienda = ? ORDER BY id DESC");
$stmt->bind_param("s", $slug);
$stmt->execute();
$result = $stmt->get_result();

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

echo json_encode(['success' => true, 'flujos' => $flujos]);
$stmt->close();
