<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/functions.php';

header('Content-Type: application/json; charset=utf-8');
$conn = db();

$codigo = parseCodigo($_GET['codigo'] ?? null);
if ($codigo === null) {
    echo json_encode(['ok' => false, 'error' => 'Código vacío']);
    exit;
}

$stmt = $conn->prepare('SELECT codigo, titulo, iva FROM productos WHERE codigo = ?');
$stmt->bind_param('s', $codigo);
$stmt->execute();
$producto = $stmt->get_result()->fetch_assoc();
$stmt->close();

if (!$producto) {
    echo json_encode(['ok' => false, 'error' => 'Producto no encontrado']);
    exit;
}

echo json_encode(['ok' => true, 'producto' => $producto], JSON_UNESCAPED_UNICODE);
