<?php
// ajax/bit_meters.ajax.php
// Returns JSON array of { serial, meters } sorted by meters desc.
// Query params:
//   type    = 'DRILLING BIT' | 'RIEMING BIT'
//   user_id = (optional) filter to a specific user
include $_SERVER['DOCUMENT_ROOT'] . "/root.class.php";
include $_SERVER['DOCUMENT_ROOT'] . "/app/test/classes/dashboard.class.php";

header('Content-Type: application/json');

$db      = new db_safeguard();
$dash    = new DashboardData($db);

// Whitelist the type to prevent injection
$allowed = ['DRILLING BIT', 'RIEMING BIT'];
$type    = $_GET['type'] ?? '';

if (!in_array($type, $allowed)) {
    echo json_encode([]);
    exit;
}

$user_id = isset($_GET['user_id']) ? intval($_GET['user_id']) : 0;

echo json_encode($dash->bitMeters($type, $user_id));