<?php
require_once __DIR__ . '/../auth.php';
if(function_exists('require_login'))require_login('login.php');
if(function_exists('require_any_perm'))require_any_perm(['can_view_dashboard_summary','can_view_dashboard_sectors','can_view_dashboard_receivers']);
if(session_status()===PHP_SESSION_ACTIVE)@session_write_close();
require_once __DIR__ . '/../db_bootstrap.php';
require_once __DIR__ . '/../device_subscriber_confirmation_lib.php';
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-store, max-age=0');
@ini_set('display_errors','0');
try{
  $conn=$GLOBALS['conn']??ensure_db(true);if(!($conn instanceof mysqli))throw new RuntimeException('db');
  $raw=(string)($_GET['items']??'');$requests=[];
  foreach(array_filter(explode(',',$raw)) as $token){
    if(preg_match('/^(routers|sectors|receivers):(\d+)$/i',trim($token),$m))$requests[]=['group'=>strtolower($m[1]),'id'=>(int)$m[2]];
  }
  echo json_encode(ua_device_subscriber_confirm_batch($conn,$requests),JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
}catch(Throwable $e){http_response_code(500);echo json_encode(['ok'=>false,'items'=>[],'error'=>'confirmation_unavailable']);}
