<?php
// ─── GET /api/pumps/installs-list.php ───────────────────────────────────
define('ROOT', dirname(__DIR__, 2));
require_once ROOT . '/core/DB.php';
require_once ROOT . '/core/Response.php';
require_once ROOT . '/core/Auth.php';

Auth::require();
$db = DB::get();

$status    = trim($_GET['status']    ?? '');
$client    = trim($_GET['client']    ?? '');
$jobcard   = trim($_GET['jobcard']   ?? '');

$where  = ['1=1'];
$params = [];

if ($status !== '')  { $where[] = 'pi.status = ?';              $params[] = (int) $status; }
if ($client !== '')  { $where[] = 'pi.client_name LIKE ?';      $params[] = "%$client%"; }
if ($jobcard !== '') { $where[] = 'pi.drilling_invoice LIKE ?'; $params[] = "%$jobcard%"; }

$rows = $db->rows(
    "SELECT pi.*,
            t.name AS team_name,
            j.client_name AS jc_client_name,
            j.record_id   AS jc_record_id
     FROM   pump_installation pi
     LEFT JOIN teams  t ON t.record_id  = pi.drilling_team_id
     LEFT JOIN jobcards j ON j.jc_no   = pi.drilling_invoice
     WHERE  " . implode(' AND ', $where) . "
     ORDER BY pi.record_id DESC
     LIMIT 300",
    $params
);

Response::ok($rows);