<?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->query = $this->loadService(ProductCategoryRepositoryQuery::class);
    }

    public function getDefaultOptionsSearch(): array
    {
        return [
            'with' => [],
            'join_with' => [],
            'orderby' => '',
            'attribute_id_producer' => 'id_producer'
        ];
    }

    public function findOneProductCategoryById(int $id)
    {
        return ProductCategory::searchOne(['id' => $id]);
    }

    public function findProductCategories()
    {
        return ProductCategory::searchAll([
            'id_producer' => $this->getProducerContext()->id
        ],
            [
                'orderby' => 'product_category.position ASC'
            ]
        );
    }

    public function findProductCategoriesAsArray()
    {
        return ProductCategory::searchAll(
            [],
            [
                'orderby' => 'product_category.position ASC',
                'as_array' => true
            ]
        );
    }

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

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

        return $productCategoriesArrayDropdown;
    }
}