<?php

namespace DMD\LaLigaApi\Service\User\Handlers;

use DMD\LaLigaApi\Dto\UserDto;
use DMD\LaLigaApi\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;

class UserSaver
{
    public function __construct(
        public EntityManagerInterface $entityManager,
        public UserPasswordHasherInterface $passwordHasher,
    )
    {}

    public function save(User $user, UserDto $userDto): User
    {
        if (!empty($userDto->email))
        {
            $user->setEmail($userDto->email);
        }
        if (!empty($userDto->password))
        {
            $hashedPassword = $this->passwordHasher->hashPassword($user, $userDto->password);
            $user->setPassword($hashedPassword);
        }
        if (!empty($userDto->firstName))
        {
            $user->setFirstName($userDto->firstName);
        }
        if (!empty($userDto->lastName))
        {
            $user->setLastName($userDto->firstName);
        }
        if (!empty($userDto->birthday))
        {
            $user->setBirthday($userDto->birthday);
        }
        if (!empty($userDto->phone))
        {
            $user->setPhone($userDto->phone);
        }
        if (!empty($userDto->profilePicture))
        {
            $user->setProfilePicture($userDto->profilePicture);
        }
        $this->entityManager->persist($user);
        return $user;
    }
}