<?php
// public/barcode.php
require_once __DIR__ . '/../helpers/barcode_utils.php';

header('Content-Type: application/json');

$input = $_GET['value'] ?? '';
$input = digits_only($input);

if ($input === '') {
    echo json_encode(['ok' => false, 'message' => 'No barcode provided']);
    exit;
}

if (strlen($input) === 12 && is_valid_upc_a($input)) {
    echo json_encode(['ok' => true, 'type' => 'UPC-A', 'value' => $input]);
    exit;
}

echo json_encode(['ok' => false, 'message' => 'Invalid barcode']);