<?php
// ─── POST /api/pumps/repair-update.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();

$id               = (int)    ($_POST['id']                    ?? 0);
$drilling_team_id = (int)    ($_POST['drilling_team_id']      ?? 0);
$contact_number   = trim($_POST['contact_number']             ?? '');
$address          = trim($_POST['address']                    ?? '');
$repaired_by      = trim($_POST['repaired_by']                ?? '');
$problem_desc     = trim($_POST['problem_description']        ?? '');
$problem_sol      = trim($_POST['problem_solution']           ?? '');
$map_coords       = trim($_POST['map_co_ordinates']           ?? '');
$jc_status        = trim($_POST['jc_current_status']          ?? '');
$status           = (int)    ($_POST['status']                ?? 0);
$date_started     = trim($_POST['date_time_started']          ?? '');
$date_finished    = trim($_POST['date_time_finished']         ?? '');

if (!$id) Response::error('id is required.');

if ($status == 1 && !$date_finished) $date_finished = date('Y-m-d H:i');

$db->run(
    "UPDATE pump_repair SET
        drilling_team_id    = ?, contact_number  = ?, address           = ?,
        repaired_by         = ?, problem_description = ?, problem_solution = ?,
        map_co_ordinates    = ?, jc_current_status   = ?, status          = ?,
        date_time_started   = ?, date_time_finished  = ?
     WHERE record_id = ?",
    [$drilling_team_id ?: null, $contact_number, $address,
     $repaired_by, $problem_desc, $problem_sol,
     $map_coords, $jc_status, $status,
     $date_started ?: null, $date_finished ?: null, $id]
);

Response::ok(null, 'Repair updated.');