<?php

declare(strict_types=1);

namespace App\Framework\Database\Example;

use App\Framework\Database\Repository\EntityRepository;

/**
 * Beispiel für Repository mit User-Entity (Komposition statt Vererbung)
 */
final readonly class UserRepository
{
    public function __construct(
        private EntityRepository $entityRepository
    ) {
    }

    /**
     * Findet User nach Email
     */
    public function findByEmail(string $email): ?User
    {
        return $this->entityRepository->findOneBy(User::class, ['email' => $email]);
    }

    /**
     * Erstellt einen neuen User
     */
    public function create(string $name, ?string $email = null): User
    {
        $user = new User($name, $email);

        return $this->entityRepository->save($user);
    }

    /**
     * Ändert den Namen eines Users
     */
    public function changeName(User $user, string $newName): User
    {
        $updatedUser = $user->withName($newName);

        return $this->entityRepository->save($updatedUser);
    }

    /**
     * Ändert die Email eines Users
     */
    public function changeEmail(User $user, ?string $newEmail): User
    {
        $updatedUser = $user->withEmail($newEmail);

        return $this->entityRepository->save($updatedUser);
    }
}
