<?php
// Mostrar errores en desarrollo
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");

// Configuración de DB
require_once(__DIR__ . '/../config.php');

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

if (!$slug) {
    echo json_encode(['success' => false, 'message' => '❌ Falta el parámetro slug_tienda']);
    exit;
}

$stmt = $mysqli->prepare("SELECT telefono, nombre FROM contactos WHERE slug_tienda = ?");
$stmt->bind_param("s", $slug);
$stmt->execute();
$result = $stmt->get_result();

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

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

$stmt->close();
