<?php
require_once dirname(__DIR__) . '/_bootstrap.php';
require_once dirname(__DIR__, 2) . '/common/web_session_auth.php';
require_once dirname(__DIR__, 2) . '/common/scanner_service.php';

$master = golder_master_pdo();
$ctx = golder_require_current_web_customer_session($master);
$master && golder_ensure_scanner_table($master);
$deviceId = (int)($ctx['session']['device_id'] ?? 0);

// Süresi geçenleri temizle
$master->exec("UPDATE remote_scan_requests SET status = 'cancelled' WHERE status = 'pending' AND expires_at < UTC_TIMESTAMP()");

$st = $master->prepare("SELECT id FROM remote_scan_requests WHERE target_device_id = ? AND status = 'pending' ORDER BY created_at DESC LIMIT 1");
$st->execute([$deviceId]);
$row = $st->fetch();

if ($row) {
    golder_json_response(['status' => 'ok', 'has_request' => true, 'request_id' => $row['id']]);
} else {
    golder_json_response(['status' => 'ok', 'has_request' => false]);
}


