<?php

namespace CouponsPlus\App\Conditions\BuiltIn\Conditions;

use CouponsPlus\App\Conditions\Condition;
use CouponsPlus\Original\Collections\Collection;
use CouponsPlus\Original\Collections\Mapper\Types;

#[\AllowDynamicProperties]
Class UserRole extends Condition
{
    const TYPE = 'UserRole';

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

    static public function getDescription() : string
    {
        return '';
    }
    
    static public function getUseCases(): Collection
    {
        return new Collection([
            __('Restrict offers to custom roles.', 'coupons-plus-for-woocommerce'),
            __('Different discounts (tiered) depending on the user role.', 'coupons-plus-for-woocommerce'),
            __('Exclude specific user roles.', 'coupons-plus-for-woocommerce'),
        ]);    
    } 

    // desc:
    // USER MUST BE LOGGED IN (AKA HAVE AN ACCOUNT) FOR THESE CHECKS TO WORK, 
    // IF THE USER IS NOT LOGGED IN (AKA A SIMPLE GUEST/VISITOR), THIS CONDITION WILL ALWAYS FAIL REGARDLESS OF 
    // THE OPTIONS SET.
    public static function getOptions() : Collection
    {
        return new Collection([
            'roles' => Types::COLLECTION()->allowed(
                Collection::create(wp_roles()->roles)->getKeys()->asArray()
            )->meta([]),
            'inclusionType' => Types::STRING()->allowed([
                                                __('Eligible', 'coupons-plus-for-woocommerce') => 'allowed',
                                                __('Not Eligible', 'coupons-plus-for-woocommerce') => 'forbidden'
                                               ])
                                              ->withDefault('allowed')
                                              ->meta([
                                                  'alternativeLabels' => [
                                                      /*'allowed' => __('Allowed', 'coupons-plus-for-woocommerce'),
                                                      'forbidden' => __('Not Allowed', 'coupons-plus-for-woocommerce'),*/
                                                      'allowed-except-forbidden' => __('Allowed except...', 'coupons-plus-for-woocommerce'),
                                                  ],
                                                  'extraLabels' => [
                                                      'allowed' => [
                                                          'placeholder' => __('Select user roles to allow...', 'coupons-plus-for-woocommerce'),
                                                          'aboveList' => __('Only accounts with any of these roles will be allowed:', 'coupons-plus-for-woocommerce')
                                                      ],
                                                      'forbidden' => [
                                                          'placeholder' => __('Select user roles to exclude...', 'coupons-plus-for-woocommerce'),
                                                          'aboveList' => __('Accounts with any of these roles will be excluded:', 'coupons-plus-for-woocommerce')
                                                      ]
                                                  ]
                                              ]),
            //'allowGuestsOnForbidden' => Types::BOOLEAN()->withDefault(false)
        ]);
    }

    protected function test() : bool
    {
        (object) $user = wp_get_current_user();

        (boolean) $userHasSpecifiedRoles = $this->options->roles->containEither($user->roles);

        if (!$user->exists()) {
            return false;
        }

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