<?php

namespace App\Repository;

use App\Entity\Game;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Uid\UuidV7;

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

    public function findAllValid(): array
    {
        return $this->createQueryBuilder('g')
            ->where('g.isValidByAdmin = true')
            ->orderBy('g.name')
            ->getQuery()
            ->getResult();
    }

    public function findByStrID(string $id): ?Game
    {
        if (!UuidV7::isValid($id)) {
            return null;
        }

        try {
            $uuid = UuidV7::fromString($id);
        } catch (\Throwable $e) {
            return null;
        }

        return $this->find($uuid);
    }

    //    /**
    //     * @return Genre[] Returns an array of Genre objects
    //     */
    //    public function findByExampleField($value): array
    //    {
    //        return $this->createQueryBuilder('g')
    //            ->andWhere('g.exampleField = :val')
    //            ->setParameter('val', $value)
    //            ->orderBy('g.id', 'ASC')
    //            ->setMaxResults(10)
    //            ->getQuery()
    //            ->getResult()
    //        ;
    //    }

    //    public function findOneBySomeField($value): ?Genre
    //    {
    //        return $this->createQueryBuilder('g')
    //            ->andWhere('g.exampleField = :val')
    //            ->setParameter('val', $value)
    //            ->getQuery()
    //            ->getOneOrNullResult()
    //        ;
    //    }
}

?>