<?php

namespace App\Tests\Repository;

use App\Entity\Account;
use App\Repository\AccountRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Uid\Uuid;

class AccountRepositoryTest extends KernelTestCase
{
    private ?EntityManagerInterface $entityManager;
    private ?AccountRepository $repository;

    protected function setUp(): void
    {
        $kernel = self::bootKernel();

        $this->entityManager = $kernel->getContainer()
            ->get('doctrine')
            ->getManager();
            
        $this->repository = $this->entityManager->getRepository(Account::class);
    }

    protected function tearDown(): void
    {
        parent::tearDown();
        $this->entityManager->close();
        $this->entityManager = null;
        $this->repository = null;
    }

    public function testFindAdmin()
    {
        // 1. Create Admins
        $admin = new Account();
        $admin->setEmail('admin_' . uniqid() . '@test.com');
        $admin->setRoles(['ROLE_ADMIN']);
        $admin->setPassword('password');
        $admin->setName('Admin');
        $admin->setFirstName('User');
        $admin->setUsername('admin_user_' . uniqid());
        $admin->setUuid(Uuid::v4());
        $admin->setIsActif(true);
        $admin->setIsFirstLogin(false);
        $this->entityManager->persist($admin);

        // 2. Create Non-Admin
        $user = new Account();
        $user->setEmail('user_' . uniqid() . '@test.com');
        $user->setRoles(['ROLE_USER']);
        $user->setPassword('password');
        $user->setName('User');
        $user->setFirstName('Normal');
        $user->setUsername('normal_user_' . uniqid());
        $user->setUuid(Uuid::v4());
        $user->setIsActif(true);
        $user->setIsFirstLogin(false);
        $this->entityManager->persist($user);

        $this->entityManager->flush();

        // 3. Test findAdmin
        $admins = $this->repository->findAdmin();

        $this->assertGreaterThanOrEqual(1, count($admins));
        
        $found = false;
        foreach ($admins as $a) {
            if ($a->getEmail() === 'admin@test.com') {
                $found = true;
            }
            // Ensure no user is returned (this might be tricky if other tests persist data, 
            // but we check if our non-admin is in the list)
            if ($a->getEmail() === 'user@test.com') {
                $this->fail('Non-admin user returned in findAdmin()');
            }
        }
        
        $this->assertTrue($found, 'Admin user not found in findAdmin() result');
    }

    public function testUpgradePassword()
    {
        $user = new Account();
        $user->setEmail('upgrade_' . uniqid() . '@test.com');
        $user->setRoles(['ROLE_USER']);
        $user->setPassword('old_hash');
        $user->setName('Upgrade');
        $user->setFirstName('User');
        $user->setUsername('upgrade_user_' . uniqid());
        $user->setUuid(Uuid::v4());
        $user->setIsActif(true);
        $user->setIsFirstLogin(false);
        
        $this->entityManager->persist($user);
        $this->entityManager->flush();

        $this->repository->upgradePassword($user, 'new_encoded_password');

        $updatedUser = $this->repository->find($user->getId());
        $this->assertEquals('new_encoded_password', $updatedUser->getPassword());
    }
}
