<?php

namespace domain\PointSale\SharedPointSale;

use domain\_\AbstractRepositoryQuery;
use domain\_\StatusInterface;
use domain\PointSale\PointSale\PointSale;
use domain\Producer\Producer\Producer;

class SharedPointSaleRepositoryQuery extends AbstractRepositoryQuery
{
    protected SharedPointSaleDefinition $sharedPointSaleDefinition;

    public function loadDependencies(): void
    {
        $this->loadDefinition(SharedPointSaleDefinition::class);
    }

    public function filterIsStatusOnline()
    {
        $this->andWhere(['shared_point_sale.status' => StatusInterface::STATUS_ONLINE]);
        return $this;
    }

    public function filterByProducerOfPointSale(Producer $producer): self
    {
        $this->andWhere(['point_sale.id_producer' => $producer->id]);
        return $this;
    }

    public function filterByProducerWithSharing(Producer $producer): self
    {
        $this->andWhere(['id_producer_with_sharing' => $producer->id]);
        return $this;
    }

    public function filterByPointSale(PointSale $pointSale): self
    {
        $this->andWhere('id_point_sale = :id_point_sale OR id_point_sale_with_sharing = :id_point_sale')
            ->addParams(['id_point_sale' => $pointSale->id]);
        return $this;
    }

    public function filterByProducer(Producer $producer): self
    {
        $this->andWhere('point_sale.id_producer = :id_producer OR id_producer_with_sharing = :id_producer')
            ->addParams(['id_producer' => $producer->id]);
        return $this;
    }

    public function filterIsRequest()
    {
        $this->filterIsPointSaleWithSharingIsNull()
            ->filterIsStatusOnline();
        return $this;
    }

    public function filterIsConfirmed()
    {
        $this->filterIsStatusOnline();
        $this->andWhere('id_point_sale_with_sharing IS NOT NULL AND confirmed_at IS NOT NULL AND declined_at IS NULL');
        return $this;
    }

    public function filterIsPointSaleWithSharingIsNull(): self
    {
        $this->andWhere('id_point_sale_with_sharing IS NULL');
        return $this;
    }
}