<?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->loadQuery(ProductDistributionRepositoryQuery::class);
    }

    public function getDefaultOptionsSearch(): array
    {
        return [
            self::WITH => ['product','distribution'],
            self::JOIN_WITH => [],
            self::ORDER_BY => '',
            self::ATTRIBUTE_ID_PRODUCER => ''
        ] ;
    }

    public function findOneProductDistribution(Distribution $distribution, Product $product): ?ProductDistribution
    {
        return $this->createDefaultQuery()
            ->filterByDistribution($distribution)
            ->filterByProduct($product)
            ->findOne();
    }

    public function findProductDistributionsByDistribution(Distribution $distribution): array
    {
        return $this->createDefaultQuery()
            ->filterByDistribution($distribution)
            ->find();
    }

    public function countProductDistributionsByDistribution(Distribution $distribution): int
    {
        return $this->createDefaultQuery()
            ->filterByDistribution($distribution)
            ->count();
    }
}