<?php

namespace CouponsPlus\App\Conditions\BuiltIn\Conditions;

use CouponsPlus\App\Conditions\Condition;
use CouponsPlus\Original\Collections\Collection;
use CouponsPlus\App\Coupon\Tree\Data\SelectOption;
use CouponsPlus\App\Validators\InclusionValueValidator;
use CouponsPlus\App\Coupon\Tree\Data\Abilities\PreloadableData;

#[\AllowDynamicProperties]
Class CouponsInCart extends Condition implements PreloadableData
{
    const TYPE = 'CouponsInCart';

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

    static public function getDescription() : string
    {
        return '';
    }
    
    static public function getUseCases(): Collection
    {
        return new Collection([
            __('Prevent from being used with other coupons.', 'coupons-plus-for-woocommerce'),
            __('Only use this coupon when another coupon is in the cart (dependent coupon).', 'coupons-plus-for-woocommerce'),
        ]);    
    } 

    public static function getOptions() : Collection
    {
        return InclusionValueValidator::getOptions(
            $expectedValues = [
                'meta' => [
                    'name' => __('Coupons in Cart', 'coupons-plus-for-woocommerce'),
                ],
                'allowed' => [
                    'type' => 'remote',
                    'category' => 'coupons',
                    'where' => 'rest.WooCommerce',
                    'path' => 'coupons',
                    'searchArgumentName' => 'search',
                    'excludeIdsArgumentName' => 'exclude',
                    'labelProperty' => 'code',
                    'idProperty' => 'code',
                    'arguments' => [
                        'context' => 'edit',
                    ]
                ]
            ],
            $inclusionType = [
                'default' => 'forbidden',
                'forbiddenFirst' => true,
                'meta' => [
                    'labels' => [
                        'allowed' => __('Dependent', 'coupons-plus-for-woocommerce'),
                        'forbidden' => __('Incompatible', 'coupons-plus-for-woocommerce'),
                    ],
                    'alternativeLabels' => [
                        'allowed' => __('Must be present', 'coupons-plus-for-woocommerce'),
                        'forbidden' => __('Must not be combined', 'coupons-plus-for-woocommerce'),
                        'allowed-except-forbidden' => __('Allowed except...', 'coupons-plus-for-woocommerce'),
                    ],
                    'extraLabels' => [
                        'allowed' => [
                            'placeholder' => __('Search coupon codes to require...', 'coupons-plus-for-woocommerce'),
                            'aboveList' => __('Cart must have any of these coupons:', 'coupons-plus-for-woocommerce')
                        ],
                        'forbidden' => [
                            'placeholder' => __('Search incompatible coupon codes', 'coupons-plus-for-woocommerce'),
                            'aboveList' => __('Cart must not have any of these coupons:', 'coupons-plus-for-woocommerce')
                        ]
                    ],
                    'notes' => [
                        'allowed' => [
                            'id' => 'InCategories.inclusionType',
                            'title' => __('Any combination of coupons', 'coupons-plus-for-woocommerce'),
                            'content' => [
                                __('The cart must have any combination of the selected coupons in order for this coupon to be applied', 'coupons-plus-for-woocommerce'),
                            ]
                        ],
                        'forbidden' => [
                            'id' => 'InCategories.inclusionType',
                            'title' => __('Any combination of coupons except those specified', 'coupons-plus-for-woocommerce'),
                            'content' => [
                                __('The cart must not have any of the selected coupons in order for this coupon to be applied', 'coupons-plus-for-woocommerce')
                            ]
                        ],
                    ]
                ]
            ],
        );
/*        return new Collection([
            'codes' => Types::COLLECTION(),
            'inclusionType' => Types::STRING()->allowed([
                                                __('Cart has any of these coupons', 'coupons-plus-for-woocommerce') => 'allowed', 
                                                __('Cart does not have any of these coupons', 'coupons-plus-for-woocommerce') => 'forbidden'
                                               ])
                                              ->withDefault('allowed'),
            //'allowGuestsOnForbidden' => Types::BOOLEAN()->withDefault(false)
        ]);*/
    }

    public static function preloadedData() : Collection
    {
        return new Collection([
            'category' => 'coupons',
            'data' => function (array $codes) : array {
                return Collection::create($codes)->map(
                    function (string $code) {
                        return new SelectOption($code);
                    })->asArray();
            }
        ]);
    }


    protected function test() : bool
    {
        (boolean) $cartHasCouponCode = $this->options->expectedValues->containEither(
            wc()->cart->get_applied_coupons()
        );

        return $this->options->inclusionType->is('allowed')? $cartHasCouponCode : !$cartHasCouponCode;
    }
}