<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// CORS
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Methods: POST, OPTIONS");
    header("Access-Control-Allow-Headers: Content-Type, Authorization");
    http_response_code(200);
    exit;
}

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");

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

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

if (!$id || !$slug) {
    echo json_encode(['success' => false, 'message' => '❌ Faltan parámetros (id, slug_tienda)']);
    exit;
}

$stmt = $mysqli->prepare("DELETE FROM flujos_chatbot WHERE id = ? AND slug_tienda = ?");
$stmt->bind_param("is", $id, $slug);

if ($stmt->execute()) {
    echo json_encode(['success' => true, 'message' => '✅ Flujo eliminado correctamente']);
} else {
    echo json_encode(['success' => false, 'message' => '❌ Error al eliminar flujo']);
}

$stmt->close();
