<?php
declare(strict_types=1);

require_once __DIR__ . '/../../system/bootstrap.php';
require_once __DIR__ . '/../../system/auth.php';
require_once __DIR__ . '/../../system/response.php';

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

$user = require_auth($pdo);

if ($user['role_key'] !== 'supervisor') {
    error_forbidden('Only shiftboss can access queue');
}

$limit = 50;

$stmt = $pdo->prepare("
    SELECT
        d.id,
        d.created_at,
        d.status,
        d.mine_id,
        u.name AS miner_name
    FROM md_declarations d
    JOIN users u
        ON u.id = d.created_by
    WHERE
        d.shiftboss_id = :shiftboss_id
        AND d.status = 'submitted'
    ORDER BY d.created_at DESC
    LIMIT $limit
");

$stmt->execute([
    'shiftboss_id' => $user['id']
]);

$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

success('Queue loaded', [
    'items' => $rows
]);