<?php

namespace App\Subscription\Repository;

use App\Subscription\Entity\SubscriptionPeriod;
use App\Subscription\Entity\SubscriptionPlan;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

class SubscriptionPeriodRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, SubscriptionPeriod::class);
    }

    public function findByUuid(string $uuid): ?SubscriptionPeriod
    {
        return $this->findOneBy(['uuid' => $uuid]);
    }

    public function findTrialPeriodForPlan(SubscriptionPlan $plan): ?SubscriptionPeriod
    {
        return $this->findOneBy(['plan' => $plan, 'isTrial' => true, 'active' => true]);
    }

    public function save(SubscriptionPeriod $period): void
    {
        $this->getEntityManager()->persist($period);
        $this->getEntityManager()->flush();
    }
}
