<?php

namespace App\Subscription\Repository;

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

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

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

    public function findByName(string $name): ?SubscriptionPlan
    {
        return $this->findOneBy(['name' => $name, 'active' => true]);
    }

    public function findAllActive(): array
    {
        return $this->createQueryBuilder('p')
            ->where('p.active = true')
            ->orderBy('p.level', 'ASC')
            ->getQuery()
            ->getResult();
    }

    public function findAllForAdmin(): array
    {
        return $this->createQueryBuilder('p')
            ->orderBy('p.level', 'ASC')
            ->getQuery()
            ->getResult();
    }

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