<?php

namespace App\Game\Repository;

use App\Game\Enum\SessionSettingType;
use App\Game\Entity\Player;
use App\Game\Entity\Session;
use App\Game\Entity\SessionSetting;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @extends ServiceEntityRepository<SessionSetting>
 */
class SessionSettingRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, SessionSetting::class);
    }

    public function getSetting(Session $session, SessionSettingType $name, ?Player $player = null): ?SessionSetting
    {
        $qb = $this->createQueryBuilder('s')
            ->where('s.session = :session')
            ->andWhere('s.name = :name')
            ->setParameter('session', $session)
            ->setParameter('name', $name->value);

        if ($player) {
            $qb->andWhere('s.player = :player')
                ->setParameter('player', $player);
        } else {
            $qb->andWhere('s.player IS NULL');
        }

        return $qb->getQuery()->getOneOrNullResult();
    }
}
