<?php
// ─── GET /api/pumps/repairs-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']  ?? '');

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

if ($status !== '') { $where[] = 'pr.status = ?';           $params[] = (int) $status; }
if ($client !== '') { $where[] = 'pr.address LIKE ?';       $params[] = "%$client%"; }

$rows = $db->rows(
    "SELECT pr.*,
            t.name  AS team_name,
            pi.client_name AS install_client,
            pi.area        AS install_area
     FROM   pump_repair pr
     LEFT JOIN teams            t  ON t.record_id  = pr.drilling_team_id
     LEFT JOIN pump_installation pi ON pi.record_id = pr.installation_id
     WHERE  " . implode(' AND ', $where) . "
     ORDER BY pr.record_id DESC
     LIMIT 300",
    $params
);

Response::ok($rows);