<?php

namespace CouponsPlus\App\Conditions\BuiltIn\Conditions;

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

#[\AllowDynamicProperties]
class UserID extends Condition implements PreloadableData
{
    const TYPE = 'UserID';

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

    static public function getDescription() : string
    {
        return '';
    }

    static public function getUseCases(): Collection
    {
        return new Collection([
                              ]);
    }

    // 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([
            'ids' => Types::COLLECTION()->meta([
                '__allowed' => [
                    'type' => 'remote',
                    'category' => 'users',
                    'where' => 'rest.couponsPlus',
                    'path' => 'users/search',
                    'searchArgumentName' => 'search',
                    'excludeIdsArgumentName' => 'exclude',
                    'labelProperty' => 'label',
                    'idProperty' => 'id',
                ],
            ]),
            'inclusionType' => Types::STRING()->allowed([
                __('Eligible', 'coupons-plus-for-woocommerce') => 'allowed',
                __('Not Eligible', 'coupons-plus-for-woocommerce') => 'forbidden',
            ])->withDefault('allowed')->meta([
                'extraLabels' => [
                    'allowed' => [
                        'placeholder' => __('Search users to include...', 'coupons-plus-for-woocommerce'),
                        'aboveList' => __('Only these users will be eligible:', 'coupons-plus-for-woocommerce'),
                    ],
                    'forbidden' => [
                        'placeholder' => __('Search users to exclude...', 'coupons-plus-for-woocommerce'),
                        'aboveList' => __('These users will be excluded:', 'coupons-plus-for-woocommerce'),
                    ],
                ],
            ]),
        ]);
    }

    public static function preloadedData() : Collection
    {
        return new Collection([
            'category' => 'users',
            'data' => function (array $ids) : array {
                return Collection::create($ids)->map(function ($id) {
                    $user = get_user_by('id', (int) $id);

                    if (!$user) {
                        return $id;
                    }

                    $nickname = (string) get_user_meta($user->ID, 'nickname', true);
                    $displayName = (string) $user->display_name;
                    $email = (string) $user->user_email;
                    $label = sprintf(
                        '%s (%s, %s)',
                        $displayName,
                        $nickname ?: __('No nickname', 'coupons-plus-for-woocommerce'),
                        $email
                    );

                    return new SelectOption(
                        (string) $user->ID,
                        [
                            'type' => 'user',
                            'option' => [
                                'id' => (int) $user->ID,
                                'name' => $displayName,
                                'nickname' => $nickname,
                                'email' => $email,
                            ],
                        ],
                        $label
                    );
                })->asArray();
            },
        ]);
    }

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

        $isUser = $this->options->ids->have($user->ID);

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