<?php

namespace App\Repository;

use App\DataObjects\FoodData;
use App\Entity\Ticket;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @extends ServiceEntityRepository<Ticket>
 * @method Ticket|null findOneById(int $entityId)
 */
class TicketRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Ticket::class);
    }

    public function getFoodData(): FoodData
    {
        $qb = $this->createQueryBuilder('t')
            ->select(
                'SUM(CASE WHEN t.foodType = 1 THEN 1 ELSE 0 END) as totalMeat',
                'SUM(CASE WHEN t.foodType = 2 THEN 1 ELSE 0 END) as totalVegetarian',
                'SUM(CASE WHEN t.foodType = 3 THEN 1 ELSE 0 END) as totalVegan'
            );

        $result = $qb->getQuery()->getSingleResult();

        return new FoodData(
            (int) $result['totalMeat'],
            (int) $result['totalVegetarian'],
            (int) $result['totalVegan']
        );
    }
}
