<?php

namespace goals\traits;

use objects\users_o;

trait goals_criteria_users_t
{
    /**
     * @var users_o[] The Users / customers to be included in the criteria
     */
    protected array $list = [];

    /**
     * @inheritDoc
     */
    public function set(array $users): self
    {
        $this->list = $users;
        return $this;
    }
    /**
     * @inheritDoc
     */
    public function list(): array
    {
        return $this->list;
    }

    /**
     * @inheritDoc
     */
    public function listCustomerNumbers(): array
    {
        return array_map(
            function (users_o $user) {
                return (int)$user->customer_number->value();
            },
            $this->list()
        );
    }

    /**
     * @inheritDoc
     */
    public function add(users_o|array $users): self
    {
        if (is_array($users)) {
            $this->list = array_merge($this->list, $users);
        } else {
            $this->list[] = $users;
        }
        return $this;
    }

    /**
     * @inheritDoc
     */
    public function clear(): self
    {
        $this->list = [];
        return $this;
    }

    /**
     * @inheritDoc
     */
    public function remove(users_o|array $users): self
    {
        if (is_array($users)) {
            $this->list = array_filter($this->list, function ($user) use ($users) {
                return !in_array($user, $users, true);
            });
        } else {
            $this->list = array_filter($this->list, function ($user) use ($users) {
                return $user !== $users;
            });
        }
        return $this;
    }
}