<?php
require_once __DIR__ . '/../app/bootstrap.php';
use App\Core\PosService; use App\Core\Response;
PosService::requireAccess('public/login.php');
$vendorId = (int)(PosService::resolveVendorId((int)($_GET['vendor_id'] ?? 0)) ?? 0);
if ($vendorId < 1) Response::json(['ok'=>false,'message'=>'No approved vendor is assigned to this POS session.'],403);
$code = trim((string)($_GET['code'] ?? ''));
if ($code !== '') {
    $product = PosService::productByCode(mb_substr($code,0,190), $vendorId);
    Response::json($product ? ['ok'=>true,'product'=>$product] : ['ok'=>false,'message'=>'No product matched this barcode/SKU.'], $product ? 200 : 404);
}
$q = mb_substr(trim((string)($_GET['q'] ?? '')),0,120);
Response::json(['ok'=>true,'products'=>PosService::searchProducts($q, $vendorId, 60)]);
