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

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

    public function findOneProductPriceById(int $id): ?ProductPrice
    {
        return ProductPrice::searchOne(['id' => $id]);
    }

    public function findOneProductPriceBy(Product $product, User $user = null, UserGroup $userGroup = null, PointSale $pointSale = null, float $fromQuantity = null)
    {
        $params = ['id_product' => $product->id];

        if($user) {
            $params['id_user'] = $user->id;
        }

        if($userGroup) {
            $params['id_user_group'] = $userGroup->id;
        }

        if($pointSale) {
            $params['id_point_sale'] = $pointSale->id;
        }

        if($fromQuantity) {
            $params['from_quantity'] = $fromQuantity;
        }

        return ProductPrice::searchOne($params);
    }
}