<?php

declare(strict_types=1);

namespace App\Tests\Unit\Repository;

use App\Entity\User;
use App\Repository\UserRepository;
use App\Tests\AppKernelTestCase;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;

class UserRepositoryTest extends AppKernelTestCase
{
    private UserRepository $repo;

    protected function setUp(): void
    {
        parent::setUp();
        $this->repo = static::getContainer()->get(UserRepository::class);
    }

    // UR-01: valid User → password updated in DB
    public function test_upgrade_password_updates_password_for_valid_user(): void
    {
        $user = $this->createUser('ur01@example.com', 'oldpassword');
        $userId = $user->getId();

        $this->repo->upgradePassword($user, 'newhashed_password');

        $this->em()->clear();
        $reloaded = $this->em()->find(User::class, $userId);
        $this->assertSame('newhashed_password', $reloaded->getPassword());
    }

    // UR-02: non-User PasswordAuthenticatedUserInterface → throws UnsupportedUserException
    public function test_upgrade_password_throws_for_non_user(): void
    {
        $fakeUser = new class implements PasswordAuthenticatedUserInterface {
            public function getPassword(): ?string { return null; }
            public function getUserIdentifier(): string { return 'fake'; }
            public function getRoles(): array { return []; }
            public function eraseCredentials(): void {}
        };

        $this->expectException(UnsupportedUserException::class);
        $this->repo->upgradePassword($fakeUser, 'newpassword');
    }
}
