<?php
require_once __DIR__ . '/../config/db.php';
require_once __DIR__ . '/../config/auth.php';

$user = requireAuth();
$db = getDB();
$id = (int) post('id', 0);
if (!$id)
    apiError('Job card ID required.', 422);

$stmt = $db->prepare("
    SELECT jc.*,
           c.company_name     AS client_name,
           u.full_name        AS assigned_name,
           p.name             AS project_name,
           v.registration     AS vehicle_reg,
           v.make             AS vehicle_make,
           v.model            AS vehicle_model,
           v.cost_per_km      AS vehicle_cost_per_km,
           v.quote_rate_per_km AS vehicle_quote_per_km
    FROM job_cards jc
    LEFT JOIN clients        c ON c.id = jc.client_id
    LEFT JOIN users          u ON u.id = jc.assigned_to
    LEFT JOIN projects       p ON p.id = jc.project_id
    LEFT JOIN fleet_vehicles v ON v.id = jc.vehicle_id
    WHERE jc.id = ?
");
$stmt->execute([$id]);
$jc = $stmt->fetch();
if (!$jc)
    apiError('Job card not found.', 404);

// Technicians
$s = $db->prepare("SELECT jct.*, u.full_name FROM job_card_technicians jct JOIN users u ON u.id = jct.user_id WHERE jct.job_card_id = ?");
$s->execute([$id]);
$jc['technicians'] = $s->fetchAll();

// Time logs
$s = $db->prepare("SELECT jctl.*, u.full_name AS user_name FROM job_card_time_logs jctl JOIN users u ON u.id = jctl.user_id WHERE jctl.job_card_id = ? ORDER BY jctl.event_time ASC");
$s->execute([$id]);
$jc['time_logs'] = $s->fetchAll();

// Notes — hide private from non-admins
$noteWhere = ($user['role_id'] == 1) ? "" : "AND (jcn.is_private = 0 OR jcn.user_id = {$user['id']})";
$s = $db->prepare("SELECT jcn.*, u.full_name AS user_name FROM job_card_notes jcn JOIN users u ON u.id = jcn.user_id WHERE jcn.job_card_id = ? $noteWhere ORDER BY jcn.created_at DESC");
$s->execute([$id]);
$jc['notes'] = $s->fetchAll();

// Images
$s = $db->prepare("SELECT * FROM job_card_images WHERE job_card_id = ? ORDER BY created_at ASC");
$s->execute([$id]);
$jc['images'] = $s->fetchAll();

// Locations
try {
    $s = $db->prepare("SELECT jcl.*, u.full_name AS user_name FROM job_card_locations jcl JOIN users u ON u.id = jcl.user_id WHERE jcl.job_card_id = ? ORDER BY jcl.captured_at ASC");
    $s->execute([$id]);
    $jc['locations'] = $s->fetchAll();
} catch (Exception $e) {
    $jc['locations'] = [];
}

// Planning
try {
    $s = $db->prepare("SELECT * FROM job_card_planning WHERE job_card_id = ? ORDER BY sort_order, id");
    $s->execute([$id]);
    $jc['planning'] = $s->fetchAll();
} catch (Exception $e) {
    $jc['planning'] = [];
}

// Checklist
try {
    $s = $db->prepare("SELECT * FROM job_card_checklist WHERE job_card_id = ? ORDER BY sort_order, id");
    $s->execute([$id]);
    $jc['checklist'] = $s->fetchAll();
} catch (Exception $e) {
    $jc['checklist'] = [];
}

// Has submitted report (signature captured)
$rCheck = $db->prepare("SELECT id FROM job_card_reports WHERE job_card_id=? LIMIT 1");
$rCheck->execute([$id]);
$jc['has_report'] = (bool) $rCheck->fetch();

apiSuccess(['job_card' => $jc]);