<?php

namespace domain\Product\ProductPrice\Repository;

use domain\PointSale\PointSale\PointSale;
use domain\Product\Product\Product;
use domain\Product\ProductPrice\Model\ProductPrice;
use domain\User\User\User;
use domain\User\UserGroup\UserGroup;
use domain\_\AbstractRepository;

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();
    }
}