<?php

function decode_tagged_json($value, $prefix) {
    if (!is_string($value) || $value === '') {
        return array();
    }

    if (strpos($value, $prefix) !== 0) {
        return array();
    }

    $json = substr($value, strlen($prefix));
    $decoded = json_decode($json, true);

    return is_array($decoded) ? $decoded : array();
}

function parse_vehicle_metadata($remark) {
    $meta = decode_tagged_json($remark, 'CARRTL_VEH|');

    return array(
        'details' => $meta['details'] ?? '',
        'brand' => $meta['brand'] ?? '',
        'model' => $meta['model'] ?? '',
        'year' => $meta['year'] ?? '',
        'color' => $meta['color'] ?? '',
        'fuel_type' => $meta['fuel_type'] ?? '',
        'odometer' => $meta['odometer'] ?? '',
        'note' => $meta['note'] ?? (strpos((string)$remark, 'CARRTL_VEH|') === 0 ? '' : (string)$remark),
    );
}

function vehicle_summary_text($remark) {
    $meta = parse_vehicle_metadata($remark);
    $parts = array();

    if ($meta['details'] !== '') {
        $parts[] = $meta['details'];
    } else if ($meta['brand'] !== '') {
        $parts[] = $meta['brand'];
    }
    if ($meta['details'] === '' && $meta['model'] !== '') {
        $parts[] = $meta['model'];
    }
    if ($meta['details'] === '' && $meta['year'] !== '') {
        $parts[] = $meta['year'];
    }
    if ($meta['details'] === '' && $meta['color'] !== '') {
        $parts[] = $meta['color'];
    }
    if ($meta['fuel_type'] !== '') {
        $parts[] = $meta['fuel_type'];
    }
    if ($meta['odometer'] !== '') {
        $parts[] = $meta['odometer'] . ' km';
    }
    if ($meta['note'] !== '') {
        $parts[] = $meta['note'];
    }

    return implode(' | ', $parts);
}

function parse_customer_document($ppnrc) {
    $meta = decode_tagged_json($ppnrc, 'CARRTL_DOC|');

    return array(
        'document_type' => $meta['document_type'] ?? '',
        'document_no' => $meta['document_no'] ?? ((strpos((string)$ppnrc, 'CARRTL_DOC|') === 0) ? '' : (string)$ppnrc),
        'license_expiry' => $meta['license_expiry'] ?? '',
    );
}

function customer_document_text($ppnrc) {
    $meta = parse_customer_document($ppnrc);
    $label = trim(($meta['document_type'] !== '' ? $meta['document_type'] . ': ' : '') . $meta['document_no']);

    if ($meta['license_expiry'] !== '') {
        $label .= ' (exp ' . $meta['license_expiry'] . ')';
    }

    return trim($label);
}

function parse_booking_metadata($remark) {
    $meta = decode_tagged_json($remark, 'CARRTL_BOOK|');

    return array(
        'pickup_location' => $meta['pickup_location'] ?? '',
        'return_location' => $meta['return_location'] ?? '',
        'rental_purpose' => $meta['rental_purpose'] ?? '',
        'estimated_distance' => $meta['estimated_distance'] ?? '',
        'rental_mode' => $meta['rental_mode'] ?? 'daily',
        'pickup_datetime' => $meta['pickup_datetime'] ?? '',
        'return_datetime' => $meta['return_datetime'] ?? '',
        'rental_hours' => $meta['rental_hours'] ?? '',
        'note' => $meta['note'] ?? (strpos((string)$remark, 'CARRTL_BOOK|') === 0 ? '' : (string)$remark),
    );
}

function infer_booking_period_from_details($stayDetails) {
    $dates = array();

    if (is_array($stayDetails)) {
        foreach ($stayDetails as $detail) {
            if (isset($detail->date) && $detail->date !== '') {
                $dates[] = substr((string)$detail->date, 0, 10);
            }
        }
    }

    $dates = array_values(array_unique(array_filter($dates)));
    sort($dates);

    if (count($dates) === 0) {
        return array(
            'pickup_datetime' => '',
            'return_datetime' => '',
            'rental_hours' => 0,
        );
    }

    $pickup = $dates[0];
    $last = end($dates);
    $return = date('Y-m-d', strtotime($last . ' +1 day'));

    return array(
        'pickup_datetime' => $pickup,
        'return_datetime' => $return,
        'rental_hours' => max(24, count($dates) * 24),
    );
}
