<?php

namespace helpers;

use Exception;
use objects\product_options_o;
use objects\products_o;

class xlvask_parser_skylning_med_ro extends xlvask_product_parser
{
    use xlvask_product_parser_t;

    public function __construct()
    {
        $this->setup('Skylning med RO'); // This is the name of Spot free product(s)
    }


    /**
     * @inheritDoc
     * @throws Exception
     */
    protected function parse(xlvask_wash_item $wash_item, xlvask_usage_log $xlvask_usage_log): products_o
    {
        // This parser is used for the "Skylning med RO" product, which is a spot-free rinse.
        // There are two different products that can be used for this, depending on the vehicle type.
        // Spot Free- Varevogn (23)
        // Spot Free- Lastbil (24)
        // This means that we need to check the vehicle type and return the appropriate product.
        $primary_product_id = $xlvask_usage_log->getPrimaryItem()?->getProduct($xlvask_usage_log)?->id ?? null;
        // Get the available addons for the primary product
        $product_options = new product_options_o();
        if (empty($primary_product_id) || $product_options->isOptionAllowedOnType(23, $primary_product_id)) {
            // If the primary product is a varevogn (van), return the Spot Free- Varevogn product
            return (new products_o())->select(23); // Spot Free- Varevogn product ID
        } elseif ($product_options->isOptionAllowedOnType(24, $primary_product_id)) {
            // If the primary product is a lastbil (truck), return the Spot Free- Lastbil product
            return (new products_o())->select(24); // Spot Free- Lastbil product ID
        }
        // Throw an exception if the vehicle type is not supported
        throw new Exception('Unsupported vehicle type for Skylning med RO product. Vehicle type ID: ' . $primary_product_id . '.');
    }
}