<?php

namespace common\logic\Product\ProductPrice\Repository;

use common\logic\AbstractRepository;
use common\logic\PointSale\PointSale\Model\PointSale;
use common\logic\Product\Product\Model\Product;
use common\logic\Product\ProductPrice\Model\ProductPrice;
use common\logic\User\User\Model\User;
use common\logic\User\UserGroup\Model\UserGroup;

class ProductPriceRepository extends AbstractRepository
{
    protected ProductPriceRepositoryQuery $query;

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

    /**
     * Retourne les options de base nécessaires à la fonction de recherche.
     */
    public function getDefaultOptionsSearch(): array
    {
        return [
            self::WITH => ['user', 'pointSale', 'userGroup'],
            self::JOIN_WITH => ['product'],
            self::ORDER_BY => '',
            self::ATTRIBUTE_ID_PRODUCER => 'product.id_producer'
        ];
    }

    public function findOneProductPriceById(int $id): ?ProductPrice
    {
        return $this->createDefaultQuery()
            ->filterById($id)
            ->findOne();
    }

    public function findOneProductPriceBy(Product $product, User $user = null, UserGroup $userGroup = null, PointSale $pointSale = null, float $fromQuantity = null)
    {
        $query = $this->createDefaultQuery()
            ->filterByProduct($product);

        if($user) {
            $query->filterByUser($user);
        }

        if($userGroup) {
            $query->filterByUserGroup($userGroup);
        }

        if($pointSale) {
            $query->filterByPointSale($pointSale);
        }

        if($fromQuantity) {
            $query->filterByFromQuantity($fromQuantity);
        }

        return $query->findOne();
    }
}