<?php

namespace common\logic\Distribution\Distribution\Repository;

use common\logic\AbstractRepositoryQuery;
use common\logic\Distribution\Distribution\Service\DistributionDefinition;
use common\logic\Subscription\Subscription\Model\Subscription;

class DistributionRepositoryQuery extends AbstractRepositoryQuery
{
    protected DistributionDefinition $definition;

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

    public function filterByDate(string $date): self
    {
        $this->query->andWhere(['date' => $date]);

        return $this;
    }

    public function filterByActive(bool $active): self
    {
        $this->query->andWhere(['active' => $active]);

        return $this;
    }

    public function filterByDateBetterThanToday(): self
    {
        $this->query
            ->andWhere('date > :date')
            ->addParams(['date' => date('Y-m-d') ]);

        return $this;
    }

    public function filterBySubscriptionDates(Subscription $subscription): self
    {
        $this->query
            ->andWhere('date >= :date_begin')
            ->addParams([
                'date_begin' => date('Y-m-d', strtotime($subscription->date_begin)),
            ]);

        if($subscription->date_end) {
            $this->query
                ->andWhere('date <= :date_end')
                ->addParams([
                    'date_end' => date('Y-m-d', strtotime($subscription->date_end))
                ]);
        }

        return $this;
    }
}