<?php

namespace App\UserProfile\Repository;

use App\Auth\Entity\User;
use App\UserProfile\Entity\UserProfile;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

class UserProfileRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, UserProfile::class);
    }

    public function findByUser(User $user): ?UserProfile
    {
        return $this->findOneBy(['user' => $user]);
    }

    public function findByUuid(string $uuid): ?UserProfile
    {
        return $this->findOneBy(['uuid' => $uuid]);
    }

    public function findOneByNationalCode(string $nationalCode): ?UserProfile
    {
        return $this->findOneBy(['nationalCode' => $nationalCode]);
    }

    public function save(UserProfile $profile, bool $flush = true): void
    {
        $this->getEntityManager()->persist($profile);
        if ($flush) {
            $this->getEntityManager()->flush();
        }
    }

    public function remove(UserProfile $profile, bool $flush = true): void
    {
        $this->getEntityManager()->remove($profile);
        if ($flush) {
            $this->getEntityManager()->flush();
        }
    }
}
