<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

include_once '../config/database.php';
include_once '../objects/vehicle.php';
include_once '../helpers/rental_metadata.php';

$database = new Database();
$db = $database->getConnection();

$vehicle = new Vehicle($db);
$stmt = $vehicle->getAllRows();

$response = array();
$response["records"] = array();

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $vehicle_meta = parse_vehicle_metadata($row['remark']);
    $vehicle_details = $vehicle_meta['details'] !== '' ? $vehicle_meta['details'] : trim(implode(' ', array_filter(array(
        $vehicle_meta['brand'],
        $vehicle_meta['model'],
        $vehicle_meta['year'],
        $vehicle_meta['color'],
    ))));
    $odometer = $vehicle_meta['odometer'] !== '' ? $vehicle_meta['odometer'] . ' km' : '';

    $response["records"][] = array(
        (int)$row['id'],
        $row['vehicle_no'],
        $row['vehicle_category'],
        $row['vehicle_note'],
        $row['seats'],
        $vehicle_details,
        $vehicle_meta['fuel_type'],
        $odometer,
        $vehicle_meta['note'],
    );
}

http_response_code(200);
echo json_encode($response);
?>
