<?php

namespace CouponsPlus\App\Conditions\BuiltIn\Conditions;

use CouponsPlus\App\Conditions\BuiltIn\Conditions\Base\Subtotal;
use CouponsPlus\App\Conditions\Condition;
use CouponsPlus\App\Quantities\AmountValidator;
use CouponsPlus\Original\Collections\Collection;
use CouponsPlus\Original\Collections\Mapper\Types;
use CouponsPlus\App\Coupon\Tree\Data\Transformers\Abilities\TreeNodeAlgorithmResolver;

#[\AllowDynamicProperties]
Class CartSubtotal extends Subtotal
{
    const TYPE = 'CartSubtotal';

    static public function getName() : string
    {
        return __('Cart Subtotal', 'coupons-plus-for-woocommerce');
    }

    static public function getDescription() : string
    {
        return '';
        //return 'Uses the subtotal of the whole cart. *DOES NOT* CALCULATE THE TOTAL BASED ON THE FILTERED ITEMS, USE A FILTER FOR THAT.';
    }

    static public function getUseCases(): Collection
    {
        return new Collection([
            __('Apply an offer on a minimum cart subtotal.', 'coupons-plus-for-woocommerce'),
            __('Apply different offers depending on the cart subtotal.', 'coupons-plus-for-woocommerce'),
        ]);    
    }

    public static function getOptions() : Collection
    {
        try {
            // since this might be used in the context of the cart, and we're in the admin,
            // let's wrap it in a try-catch to avoid errors in case there are plugins that hook into this option
            // and expect the cart to be available.
            $includeTaxesInPrice = wc_tax_enabled() && (get_option( 'woocommerce_prices_include_tax' ) === 'yes');
        } catch (\Throwable $e) {
            $includeTaxesInPrice = wc_tax_enabled() && (wp_cache_get('woocommerce_prices_include_tax', 'options') === 'yes');
        }

        //$includeTaxesInPrice = wc_tax_enabled() && get_option('woocommerce_prices_include_tax') === 'yes';
        $calculationMessage = $includeTaxesInPrice? [
            __('Includes: Cart Subtotal of all items + taxes (because you have the option to display prices with taxes)', 'coupons-plus-for-woocommerce'),
            __('Excludes: Shipping and additional fees.', 'coupons-plus-for-woocommerce'),
        ] : [
            __('Includes: Cart Subtotal of all items without taxes', 'coupons-plus-for-woocommerce'),
            __('Excludes: Taxes, shipping and additional fees.', 'coupons-plus-for-woocommerce'),
        ];

        return AmountValidator::getOptions([
            'equals' => $calculationMessage,
            'minimum' => $calculationMessage,
            'maximum' => $calculationMessage,
            'range' => $calculationMessage,
        ]);
    }

    protected function getAmountOptions() : array
    {
        return $this->options->asArray();
    }
    
    protected function getSubtotal() : float
    {
        (object) $cart = WC()->cart;

        if ($cart->display_prices_including_tax()) {
            return $cart->get_subtotal() + $cart->get_subtotal_tax();
        }

        return $cart->get_subtotal();
    }
}