<?php

namespace common\logic\Product\ProductCategory\Repository;

use common\logic\AbstractRepository;
use common\logic\Product\ProductCategory\Model\ProductCategory;

class ProductCategoryRepository extends AbstractRepository
{
    protected ProductCategoryRepositoryQuery $query;

    public function loadDependencies(): void
    {
        $this->loadQuery(ProductCategoryRepositoryQuery::class);
    }

    public function getDefaultOptionsSearch(): array
    {
        return [
            self::WITH => [],
            self::JOIN_WITH => [],
            self::ORDER_BY => 'product_category.position ASC',
            self::ATTRIBUTE_ID_PRODUCER => 'id_producer'
        ];
    }

    public function findOneProductCategoryById(int $id)
    {
        return $this->createDefaultQuery()
            ->filterById($id)
            ->findOne();
    }

    public function findProductCategories()
    {
        return $this->createDefaultQuery()->find();
    }

    public function findProductCategoriesAsArray(): array
    {
        return $this->createDefaultQuery()
            ->asArray()
            ->find();
    }

    public function populateProductCategoriesDropdownList(): array
    {
        $productCategoriesArrayDropdown = ['' => '--'];
        $productCategoriesArray = $this->findProductCategories();

        foreach ($productCategoriesArray as $productCategory) {
            $productCategoriesArrayDropdown[$productCategory['id']] = $productCategory['name'];
        }

        return $productCategoriesArrayDropdown;
    }
}