<?php

namespace helpers;

use objects\xlvask_vehicle_types_o;

class xlvask_vehicle_types extends xlvask_helper
{
    /**
     * Get a vehicle type by its ID.
     * @param string $vehicleTypeId The unique identifier for the vehicle type.
     * @return xlvask_vehicle_type|null The vehicle type object or null if not found.
     * @see xlvask_vehicle_type
     * @note This method retrieves the vehicle type from the list of vehicle types.
     */
    public static function getVehicleTypeById(string $vehicleTypeId): ?xlvask_vehicle_type
    {
        $vehicleTypes = self::getVehicleTypes();
        foreach ( $vehicleTypes as $vehicleType ) {
            if ($vehicleType->vehicleTypeId === $vehicleTypeId) {
                return $vehicleType;
            }
        }
        return null; // Return null if the vehicle type is not found
    }

    /**
     * Get the list of vehicle types that relate to the XLVask system.
     * @return array<xlvask_vehicle_type>
     * @note This is a placeholder method. The actual implementation should fetch the vehicle types from the XLVask API or database.
     * @see xlvask_vehicle
     * @see xlvask_vehicle_type
     */
    public static function getVehicleTypes(): array
    {
        return (new xlvask_vehicle_types_o())->listObjects(function ($vehicleType) {
            $vehicleType = (object)$vehicleType; // Ensure we are working with an object
            $tmp = new xlvask_vehicle_type();
            // Assign the properties shared by the xlvask_vehicle_type class.
            foreach ( $vehicleType as $key => $value ) {
                if (property_exists($tmp, $key)) {
                    $tmp->{$key} = $value;
                }
            }
            return $tmp;
        });
    }

    /**
     * Get the vehicle type by its name.
     * @param string $name The name of the vehicle type.
     * @return xlvask_vehicle_type|null The vehicle type object or null if not found.
     * @see xlvask_vehicle_type
     * @note This method retrieves the vehicle type from the list of vehicle types.
     */
    public static function getVehicleTypeByName(string $name): ?xlvask_vehicle_type
    {
        $vehicleTypes = self::getVehicleTypes();
        foreach ( $vehicleTypes as $vehicleType ) {
            if ($vehicleType->name === $name) {
                return $vehicleType;
            }
        }
        return null; // Return null if the vehicle type is not found
    }

    /**
     * Get the vehicle types with a specific product ID.
     * @param int $productId The product ID to filter the vehicle types.
     * @return array<xlvask_vehicle_type> An array of vehicle types that match the product ID.
     * @see xlvask_vehicle_type
     * @note This method retrieves the vehicle types from the list of vehicle types and filters them by product ID.
     */
    public static function getVehicleTypesByProductId(int $productId): array
    {
        $vehicleTypes = self::getVehicleTypes();
        $filteredVehicleTypes = [];
        foreach ( $vehicleTypes as $vehicleType ) {
            if ($vehicleType->product === $productId) {
                $filteredVehicleTypes[] = $vehicleType;
            }
        }
        return $filteredVehicleTypes; // Return the filtered vehicle types
    }

}