<?php

namespace App\Repository;

use App\Entity\Revendeur;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

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

    public function generateUniqueCode(): string
    {
        do {
            $code = 'EC-'.str_pad((string) random_int(0, 9999), 4, '0', STR_PAD_LEFT);
        } while (null !== $this->findOneBy(['codeRevendeur' => $code]));

        return $code;
    }
}
