<?php

namespace domain\Subscription\Subscription;

use domain\User\User\User;
use domain\_\AbstractRepositoryQuery;

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 filterByCreatedOrUpdatedByUserDuringPastWeek(): self
    {
        $datePastWeek = new \DateTime('-7 days');
        $this->andWhere('(subscription.id_user = subscription.id_created_by AND subscription.created_at >= :date_past_week) OR (subscription.id_user = subscription.id_updated_by AND subscription.updated_at >= :date_past_week)')
            ->addParams(['date_past_week' => $datePastWeek->format('Y-m-d H:i:s')]);
        return $this;
    }
}