<?php

namespace common\logic\Order\Order\Repository;

use common\logic\AbstractRepositoryQuery;
use common\logic\Document\DeliveryNote\Model\DeliveryNote;
use common\logic\Document\Invoice\Model\Invoice;
use common\logic\Order\Order\Service\OrderDefinition;
use common\logic\User\User\Model\User;

class OrderRepositoryQuery extends AbstractRepositoryQuery
{
    protected OrderDefinition $definition;

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

    public function filterById(int $id): self
    {
        $this->andWhere(['order.id' => $id]);

        return $this;
    }

    public function filterByOrigin(string $origin)
    {
        $this->andWhere(['origin' => $origin]);

        return $this;
    }

    public function filterByDate(\DateTime $date)
    {
        $this
            ->andWhere('`order`.date >= \'' .$date->format('Y-m-d 00:00:00').'\'')
            ->andWhere('`order`.date <= \'' .$date->format('Y-m-d 23:59:59').'\'');

        return $this;
    }

    public function filterByDistributionDate(string $date): self
    {
        $this->andWhere(['distribution.date' => $date]);

        return $this;
    }

    public function filterIsValid(): self
    {
        $this->andWhere('date_delete IS NULL');

        return $this;
    }

    public function filterByUser(User $user): self
    {
        $this->andWhere(['order.id_user' => $user->id]);

        return $this;
    }

    public function filterIsIncoming(): self
    {
        $this->andWhere('distribution.date >= :date_today')
            ->params([':date_today' => date('Y-m-d')]);

        return $this;
    }

    public function filterIsPassed(): self
    {
        $this->andWhere('distribution.date < :date_today')
            ->params([':date_today' => date('Y-m-d')]);

        return $this;
    }

    public function filterIsInvoiced(Invoice $invoice): self
    {
        $this->andWhere('order.id_invoice IS NOT NULL AND order.id_invoice = :id_invoice')
            ->params([':id_invoice' => $invoice->id]);

        return $this;
    }

    public function filterIsLinkedDeliveryNote(DeliveryNote $deliveryNote): self
    {
        $this->andWhere('order.id_delivery_note IS NOT NULL AND order.id_delivery_note = :id_delivery_note')
            ->params([':id_delivery_note' => $deliveryNote->id]);

        return $this;
    }

    public function filterIsNotInvoiced(): self
    {
        $this->andWhere('order.id_invoice IS NULL');

        return $this;
    }

    public function filterIsNotLinkedDeliveryNote(): self
    {
        $this->andWhere('order.id_delivery_note IS NULL');

        return $this;
    }

    public function filterIsNotIgnoreWhenInvoicing(): self
    {
        $this->andWhere('order.ignore_when_invoicing IS NULL');

        return $this;
    }

    public function orderByDistributionDate(string $order = 'ASC'): self
    {
        $this->orderBy('distribution.date '.$order );

        return $this;
    }
}