<?php

namespace helpers;

use Exception;
use objects\products_o;

trait xlvask_product_parser_t
{
    protected string $OriginalProductName;
    /**
     * This property indicates whether the parser is dynamic.
     * When set to true, the parser will handle dynamic pricing.
     * This should be set to true if the pricing of the product can change based on certain conditions,
     * such as the amount of usage, time of day, or other factors.
     * When this is set to true, the parser should implement logic to handle dynamic pricing.
     * @example
     * The "flowmeter" product parser uses this to calculate the price based on the amount of water used.
     * * @note This is used to determine whether the parser should calculate a dynamic price or a static price.
     * @see xlvask_product_parser_t::calculateDynamicPrice()
     * When this is set to false, the parser should return a static price. (amount x price per unit)
     * @var bool $useDynamicPricing
     */
    protected bool $useDynamicPricing = false;


    /**
     * Parse the Original Product Name to a product object.
     * @param xlvask_wash_item $wash_item
     * @param xlvask_usage_log $xlvask_usage_log
     * @return products_o
     * @throws Exception If the product cannot be parsed.
     */
    public function parseProduct(xlvask_wash_item $wash_item, xlvask_usage_log $xlvask_usage_log): products_o
    {
        // Make sure the Original Product Name is set
        if (empty($this->OriginalProductName)) {
            throw new Exception('The Original Product Name is not set. Please provide a valid product name.');
        }
        // Make sure this is the correct parser
        //if ($this->OriginalProductName !== $wash_item->OriginalProductName) {
        //    throw new Exception('The Original Product Name does not match the parser\'s Original Product Name. Expected: ' . $this->OriginalProductName . ', got: ' . $wash_item->OriginalProductName);
        //}
        // All checks passed, parse the product
        return $this->parse(...func_get_args());
    }

    /**
     * The actual parsing logic.
     * This method should be implemented in the class that uses this trait.
     * @param xlvask_wash_item $wash_item
     * @param xlvask_usage_log $xlvask_usage_log
     * @return products_o
     */
    abstract protected function parse(xlvask_wash_item $wash_item, xlvask_usage_log $xlvask_usage_log): products_o;

}