<?php

namespace domain\PointSale\SharedPointSale;

use domain\_\AbstractRepository;
use domain\PointSale\PointSale\PointSale;
use domain\Producer\Producer\Producer;

class SharedPointSaleRepository extends AbstractRepository
{
    protected SharedPointSaleRepositoryQuery $sharedPointSaleRepositoryQuery;

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

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

    public function findOneSharedPointSaleById(int $id)
    {
        return $this->createQuery()
            ->filterById($id)
            ->findOne();
    }

    public function findSharedPointsSaleRequestsOfMe()
    {
        return $this->createQuery()
            ->filterByProducerOfPointSale($this->getProducerContext())
            ->filterIsRequest()
            ->find();
    }

    public function findSharedPointsSaleRequestsOthers()
    {
        return $this->createQuery()
            ->filterByProducerWithSharing($this->getProducerContext())
            ->filterIsRequest()
            ->find();
    }

    public function findSharedPointsSaleConfirmedByPointSale(PointSale $pointSale, Producer $producerWithSharing = null)
    {
        $query = $this->createQuery()
            ->filterByPointSale($pointSale)
            ->filterIsConfirmed();

        if($producerWithSharing) {
            $query->filterByProducerWithSharing($producerWithSharing);
        }

        return $query->find();
    }

    public function findOneSharedPointsSaleConfirmed(PointSale $pointSale, Producer $producer)
    {
        return $this->createQuery()
            ->filterByPointSale($pointSale)
            ->filterByProducer($producer)
            ->filterIsConfirmed()
            ->findOne();
    }

    public function findSharedPointSaleRequestByPointSale(PointSale $pointSale, Producer $producerWithSharing = null)
    {
        $query = $this->createQuery()
            ->filterByPointSale($pointSale)
            ->filterIsRequest();

        if($producerWithSharing) {
            $query->filterByProducerWithSharing($producerWithSharing);
        }

        return $query->find();
    }
}