<?php
// ─── POST /api/jobcards/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';

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

// Returns (int) for numeric strings, NULL for empty
function intOrNull($val): ?int {
    $v = trim((string) $val);
    return $v === '' ? null : (int) $v;
}
// Returns trimmed string or NULL for empty
function strOrNull($val): ?string {
    $v = trim((string) $val);
    return $v === '' ? null : $v;
}

$id               = (int) ($_POST['id']               ?? 0);
$jc_no            = (int) ($_POST['jc_no']            ?? 0);
$client_name      = trim($_POST['client_name']         ?? '');
$contact_name     = trim($_POST['contact_name']        ?? '');
$contact_number   = trim($_POST['contact_number']      ?? '');
$other_number     = trim($_POST['other_number']        ?? '');
$alternate_number = trim($_POST['alternate_number']    ?? '');
$address          = trim($_POST['address']             ?? '');
$drill_coords     = trim($_POST['drill_co_ordinates']  ?? '');
$action_date      = str_replace('T', ' ', trim($_POST['action_date'] ?? ''));
$action_date      = $action_date === '' ? null : $action_date;
$team_id          = intOrNull($_POST['team_assigned_id']  ?? '');
$status           = trim($_POST['jc_current_status']   ?? '');
$water_strike     = trim($_POST['water_strike']        ?? '');
$water_flow       = trim($_POST['water_flow']          ?? '');
$compressor_hours = trim($_POST['compressor_hours']    ?? '');
$diesel_start     = (int) ($_POST['diesel_start']      ?? 0);
$diesel_stop      = (int) ($_POST['diesel_stop']       ?? 0);
$interested_pump  = trim($_POST['interested_in_pump']  ?? '');
$slip_no          = trim($_POST['slip_no']             ?? '');
$estimated_liters = (int) ($_POST['estimated_liters']  ?? 0);

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

$db->run(
    "UPDATE jobcards SET
        client_name      = ?, contact_name     = ?,
        contact_number   = ?, other_number     = ?,
        alternate_number = ?, address          = ?,
        drill_co_ordinates = ?, action_date    = ?,
        team_assigned_id = ?, jc_current_status = ?,
        water_strike     = ?, water_flow       = ?,
        compressor_hours = ?, diesel_start     = ?,
        diesel_stop      = ?, interested_in_pump = ?,
        slip_no          = ?, estimated_liters = ?
     WHERE record_id = ?",
    [$client_name, $contact_name, $contact_number, $other_number,
     $alternate_number, $address, $drill_coords, $action_date ?: null,
     $team_id ?: null, $status, $water_strike, $water_flow,
     $compressor_hours, $diesel_start, $diesel_stop, $interested_pump,
     $slip_no, $estimated_liters, $id]
);

// Mirror shared fields back to work_requests so both tables stay in sync
if ($jc_no) {
    $db->run(
        "UPDATE work_requests
         SET    contact_number   = ?,
                other_number     = ?,
                alternate_number = ?,
                address          = ?,
                team_id          = ?
         WHERE  jc_no = ?",
        [$contact_number, $other_number, $alternate_number,
         $address, $team_id ?: null, $jc_no]
    );
}

Response::ok(null, 'Jobcard updated successfully.');