<?php

namespace helpers;

use classes\xlvask;
use Exception;

class xlvask_vehicles extends xlvask_helper
{
    /**
     * Get a vehicle by its ID.
     * @param string $vehicleId The unique identifier for the vehicle.
     * @return xlvask_vehicle|null The vehicle object or null if not found.
     * @throws Exception if the XLVask API or database is not available or if there is an error in fetching the vehicles.
     * @see xlvask_vehicle
     * @note This method retrieves the vehicle from the list of vehicles.
     */
    public static function getVehicleById(string $vehicleId): ?xlvask_vehicle
    {
        $vehicles = self::getVehicles(['vehicleId' => $vehicleId]);
        foreach ( $vehicles as $vehicle ) {
            if ($vehicle->vehicleId === $vehicleId) {
                return $vehicle;
            }
        }
        return null; // Return null if the vehicle is not found
    }

    /**
     * Get the list of vehicles that relate to the XLVask system.
     * @param array $params An associative array of parameters to filter the vehicles.
     * @return array<xlvask_vehicle>
     * @note This method retrieves the vehicles from the XLVask API or database.
     * @throws Exception if the XLVask API or database is not available or if there is an error in fetching the vehicles.
     * @see xlvask_vehicle
     */
    public static function getVehicles(array $params = []): array
    {
        $xlvask = new xlvask();
        // Call the getVehicles method from the xlvask class with the provided parameters
        $vehicles = $xlvask->getVehicles(
            $params['customerId'] ?? null,
            $params['vehicleId'] ?? null,
            $params['registrationNumber'] ?? null,
            $params['changeDate'] ?? null
        );

        // Convert the result to an array of xlvask_vehicle objects
        return array_map(function ($vehicle) {
            $vehicle = (object)$vehicle; // Ensure we are working with an object
            $tmp = new xlvask_vehicle();
            // Assign the properties shared by the xlvask_vehicle class.
            foreach ( $vehicle as $key => $value ) {
                if (property_exists($tmp, $key)) {
                    $tmp->{$key} = $value;
                }
            }
            return $tmp; // Return the xlvask_vehicle object
        }, $vehicles);
    }

    /**
     * Get the vehicle by its registration number.
     * @param string $registrationNumber The registration number of the vehicle.
     * @return xlvask_vehicle|null The vehicle object or null if not found.
     * @throws Exception if the XLVask API or database is not available or if there is an error in fetching the vehicles.
     * @see xlvask_vehicle
     * @note This method retrieves the vehicle from the list of vehicles.
     */
    public static function getVehicleByRegistrationNumber(string $registrationNumber, bool $onlyCache = false): ?xlvask_vehicle
    {
        // Check if the vehicle has been cached
        $xlvask = new xlvask();
        $xlvask_cache = $xlvask->getCache();
        if ($xlvask_cache->isVehicleCached($registrationNumber)) {
            return $xlvask_cache->getVehicleCache($registrationNumber);
        } else if ($onlyCache) {
            return null; // Return null if onlyCache is true and vehicle is not cached
        }
        $vehicles = self::getVehicles(['registrationNumber' => $registrationNumber]);
        foreach ( $vehicles as $vehicle ) {
            if ($vehicle->registrationNumber === $registrationNumber) {
                return $vehicle;
            }
        }
        return null; // Return null if the vehicle is not found
    }

    /**
     * Get the vehicles by customer ID.
     * @param string $customerId The unique identifier for the customer.
     * @return array<xlvask_vehicle> An array of vehicles that belong to the customer.
     * @return array<xlvask_vehicle>
     * @throws Exception if the XLVask API or database is not available or if there is an error in fetching the vehicles.
     * @see xlvask_vehicle
     * @note This method retrieves the vehicles from the list of vehicles and filters them by customer ID.
     */
    public static function getVehiclesByCustomerId(string $customerId): array
    {
        $vehicles = self::getVehicles(['customerId' => $customerId]);
        $filteredVehicles = [];
        foreach ( $vehicles as $vehicle ) {
            if ($vehicle->customerId === $customerId) {
                $filteredVehicles[] = $vehicle;
            }
        }
        return $filteredVehicles; // Return the filtered vehicles
    }
}