<?php

namespace App\Repository;

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

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

    public function findByEmail(string $email): ?Gamemaster
    {
        return $this->createQueryBuilder('g')
            ->where('g.email = :email')
            ->setParameter('email', $email)
            ->getQuery()
            ->getOneOrNullResult()
            ;    
    }

    public function findUnlinked(): array
    {
        return $this->createQueryBuilder('g')
            ->where('g.linkToUser IS null')
            ->getQuery()
            ->getResult();
    }

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

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

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

    //    /**
    //     * @return Gamemaster[] Returns an array of Gamemaster 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): ?Gamemaster
    //    {
    //        return $this->createQueryBuilder('g')
    //            ->andWhere('g.exampleField = :val')
    //            ->setParameter('val', $value)
    //            ->getQuery()
    //            ->getOneOrNullResult()
    //        ;
    //    }
}
