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

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

// 🔹 Conexión a la base de datos
$host = "92.113.151.17";
$user = "thank";
$pass = "1541100Luis";
$db   = "shop_than";

$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
    echo json_encode(['success' => false, 'message' => 'Error de conexión DB']);
    exit;
}

// 🔹 Recibir parámetros
$slug_tienda = $_GET['slug_tienda'] ?? '';
$token_api   = $_GET['token_api'] ?? '';

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

// 🔹 Consulta de validación
$stmt = $conn->prepare("SELECT 1 FROM usuarios_tienda WHERE slug_tienda = ? AND token_api_meta = ? LIMIT 1");
$stmt->bind_param("ss", $slug_tienda, $token_api);
$stmt->execute();
$stmt->store_result();

// 🔹 Respuesta
if ($stmt->num_rows > 0) {
    echo json_encode(['success' => true]);
} else {
    echo json_encode(['success' => false, 'message' => 'Token inválido']);
}

$stmt->close();
$conn->close();
