<?php

declare(strict_types=1);

namespace App\Repository;

use App\Entity\DrinkType;
use Doctrine\ORM\EntityManagerInterface;
use Override;

/**
 * @extends AbstractRepository<DrinkType>
 */
class DrinkTypeRepository extends AbstractRepository
{
    public function __construct(EntityManagerInterface $entityManager)
    {
        parent::__construct($entityManager, $entityManager->getClassMetadata(DrinkType::class));
    }

    /** @inheritDoc */
    #[Override]
    public function findAll(): array
    {
        return parent::findBy(
            criteria: [],
            orderBy: [
                'desiredStock' => 'DESC',
            ],
        );
    }

    /** @return DrinkType[] */
    public function findDesired(): array
    {
        $qb = $this->getEntityManager()->createQueryBuilder();
        $qb
            ->select('d')
            ->from(DrinkType::class, 'd')
            ->where('d.desiredStock > 0')
            ->orderBy('d.desiredStock', 'DESC')
            ->addOrderBy('d.name', 'ASC');

        /** @var array<int, DrinkType> $result */
        $result = $qb->getQuery()->getResult();
        return $result;
    }
}
