<?php
// CORS
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;
}

date_default_timezone_set('America/Bogota');
require_once(__DIR__ . '/../config.php');

$input = json_decode(file_get_contents("php://input"), true);

$slug = $input['slug'] ?? null;
$telefono = $input['numero'] ?? null;
$etiqueta = $input['etiqueta'] ?? null;

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

$stmt = $mysqli->prepare("INSERT INTO etiquetas_contacto (slug_tienda, telefono, etiqueta, fecha) VALUES (?, ?, ?, NOW())");
$stmt->bind_param("sss", $slug, $telefono, $etiqueta);
$success = $stmt->execute();

if ($success) {
    echo json_encode(['success' => true, 'message' => 'Etiqueta registrada correctamente']);
} else {
    echo json_encode(['success' => false, 'message' => 'Error al insertar etiqueta: ' . $stmt->error]);
}

$stmt->close();
