<?php

namespace domain\Order\Order;

use domain\Document\DeliveryNote\DeliveryNote;
use domain\Document\Invoice\Invoice;
use domain\Order\OrderStatus\OrderStatus;
use domain\User\User\User;
use domain\_\AbstractRepositoryQuery;

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 filterByDistributionDateStartEnd(\DateTime $dateStart, \DateTime $dateEnd): self
    {
        $this
            ->andWhere('distribution.date >= \'' .$dateStart->format('Y-m-d').'\'')
            ->andWhere('distribution.date <= \'' .$dateEnd->format('Y-m-d').'\'');

        return $this;
    }

    public static function getSqlFilterIsValid(): string
    {
        return " (`order`.order_status_alias = '".OrderStatus::ALIAS_ORDERED."' OR `order`.order_status_alias = '".OrderStatus::ALIAS_UPDATED."') ";
    }

    public static function getSqlFilterIsOrderStatusNotDeleted(): string
    {
        return " (`order`.order_status_alias != '".OrderStatus::ALIAS_DELETED."') ";
    }

    public function filterIsValid(): self
    {
        $this->andWhere(self::getSqlFilterIsValid());

        return $this;
    }

    public function filterIsOrderStatusNotDeleted(): self
    {
        $this->andWhere('order_status_alias != :status_deleted')
            ->addParams([':status_deleted' => OrderStatus::ALIAS_DELETED]);

        return $this;
    }

    public function filterIsOrderStatusAliasNull(): self
    {
        $this->andWhere('order_status_alias 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')
            ->addParams([':date_today' => date('Y-m-d')]);

        return $this;
    }

    public function filterIsPassed(): self
    {
        $this->andWhere('distribution.date < :date_today')
            ->addParams([':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')
            ->addParams([':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')
            ->addParams([':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;
    }
}