<?php

namespace common\logic\Subscription\Subscription\Repository;

use common\logic\AbstractRepositoryQuery;
use common\logic\Subscription\Subscription\Service\SubscriptionDefinition;
use common\logic\User\User\Model\User;

class SubscriptionRepositoryQuery extends AbstractRepositoryQuery
{
    protected SubscriptionDefinition $definition;

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

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

    public function filterByCreatedDuringPastWeek(): self
    {
        $datePastWeek = new \DateTime('-7 days');
        $this->andWhere('subscription.created_at >= :created_at')
            ->addParams(['created_at' => $datePastWeek->format('Y-m-d H:i:s')]);
        return $this;
    }

    public function filterByCreatedByUser(): self
    {
        $this->andWhere('subscription.id_user = subscription.id_created_by');
        return $this;
    }
}