<?php

namespace common\logic\Distribution\ProductDistribution\Repository;

use common\logic\AbstractRepository;
use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Distribution\ProductDistribution\Model\ProductDistribution;
use common\logic\Product\Product\Model\Product;

class ProductDistributionRepository extends AbstractRepository
{
    protected ProductDistributionRepositoryQuery $query;

    public function loadDependencies(): void
    {
        $this->query = $this->loadService(ProductDistributionRepositoryQuery::class);
    }

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

    public function findOneProductDistribution(Distribution $distribution, Product $product): ?ProductDistribution
    {
        return ProductDistribution::searchOne([
            'id_distribution' => $distribution->id,
            'id_product' => $product->id
        ]);
    }

    public function findProductDistributionsByDistribution(Distribution $distribution): array
    {
        return ProductDistribution::searchAll([
            'id_distribution' => $distribution->id
        ]) ;
    }

    public function countProductDistributionsByDistribution(Distribution $distribution): int
    {
        return ProductDistribution::searchCount([
            'id_distribution' => $distribution->id
        ]);
    }
}