<?php

namespace common\logic\Subscription\Subscription\Repository;

use common\logic\AbstractRepository;
use common\logic\Producer\Producer\Model\Producer;
use common\logic\Subscription\Subscription\Model\Subscription;
use common\logic\Subscription\Subscription\Service\SubscriptionSolver;
use common\logic\User\User\Model\User;

class SubscriptionRepository extends AbstractRepository
{
    protected SubscriptionRepositoryQuery $query;
    protected SubscriptionSolver $subscriptionSolver;

    public function loadDependencies(): void
    {
        $this->loadQuery(SubscriptionRepositoryQuery::class);
        $this->subscriptionSolver = $this->loadService(SubscriptionSolver::class);
    }

    public function getDefaultOptionsSearch(): array
    {
        return [
            self::WITH => ['producer'],
            self::JOIN_WITH => ['user', 'productSubscription', 'productSubscription.product', 'pointSale'],
            self::ORDER_BY => 'user.name ASC',
            self::ATTRIBUTE_ID_PRODUCER => 'subscription.id_producer'
        ];
    }

    public function findOneSubscriptionById(int $id): ?Subscription
    {
        return $this->createDefaultQuery()
            ->filterById($id)
            ->findOne();
    }

    public function findSubscriptions()
    {
        return $this->createDefaultQuery()->find();
    }

    /**
     * Retourne les abonnements pour une date donnée.
     */
    public function findSubscriptionsByDate(string $date)
    {
        $date = date('Y-m-d', strtotime($date));
        $subscriptionArray = $this->findSubscriptions();
        $subscriptionReturnArray = [];

        foreach ($subscriptionArray as $subscription) {
            if ($date >= $subscription->date_begin &&
                (!$subscription->date_end || $date <= $subscription->date_end)
                && $this->subscriptionSolver->isSubscriptionMatchWith($subscription, $date)) {
                $subscriptionReturnArray[] = $subscription;
            }
        }

        return $subscriptionReturnArray;
    }

    public function countSubscriptionsByUser(User $user)
    {
        return $this->createDefaultQuery()
            ->filterByUser($user)
            ->count();
    }

    public function findSubscriptionsLatestAdded()
    {
        return $this->createDefaultQuery()
            ->filterByCreatedDuringPastWeek()
            ->filterByCreatedByUser()
            ->find();
    }
}