<?php

namespace goals\classes;

use goals\interfaces\goals_criteria_products_i;
use goals\traits\goals_criteria_products_t;
use objects\order_items_o;

class goals_criteria_products implements goals_criteria_products_i
{
    use goals_criteria_products_t;

    /**
     * Get the sold items count based on the criteria.
     * @param goals_criteria $goal
     * @return int
     */
    public function count(goals_criteria $goal): int
    {
        $count = 0;
        $datetime_start = $goal->start ?? new \DateTime();
        $datetime_end = $goal->end ?? new \DateTime();

        $items = order_items_o::getListByCriteria(
            department_ids: $goal->departments->listIDs(),
            product_ids: $this->listIDs(),
            customer_numbers: $goal->users->listCustomerNumbers(),
            datetime_start: $datetime_start,
            datetime_end: $datetime_end,
        );
        foreach ($items as $item) {
            $count += (int)$item->quantity->value();
        }
        return $count;
    }

}