<?php

namespace App\Tests\Controller\Admin;

use App\Controller\Admin\MembresController;
use App\Entity\User;
use App\Repository\UserRepository;
use App\Service\KeycloakAdminService;
use App\Service\MailerService;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Twig\Environment;

class MembresControllerTest extends TestCase
{
    private function createContainer(): ContainerInterface
    {
        $session = new Session(new MockArraySessionStorage());
        $stack = $this->createStub(RequestStack::class);
        $stack->method('getSession')->willReturn($session);

        $twig = $this->createStub(Environment::class);
        $twig->method('render')->willReturn('<html></html>');

        $router = $this->createStub(RouterInterface::class);
        $router->method('generate')->willReturn('/admin/membres');

        $container = $this->createStub(ContainerInterface::class);
        $container->method('has')->willReturn(true);
        $container->method('get')->willReturnMap([
            ['twig', $twig],
            ['router', $router],
            ['security.authorization_checker', $this->createStub(AuthorizationCheckerInterface::class)],
            ['security.token_storage', $this->createStub(TokenStorageInterface::class)],
            ['request_stack', $stack],
            ['parameter_bag', $this->createStub(\Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface::class)],
        ]);

        return $container;
    }

    private function createKeycloak(array $users = [], array $groups = []): KeycloakAdminService
    {
        $kc = $this->createStub(KeycloakAdminService::class);
        $kc->method('ensureRequiredGroups')->willReturn([]);
        $kc->method('listUsers')->willReturn($users);
        $kc->method('getUserGroups')->willReturn([]);
        $kc->method('listGroups')->willReturn($groups);

        return $kc;
    }

    public function testIndexEmpty(): void
    {
        $controller = new MembresController($this->createStub(LoggerInterface::class));
        $controller->setContainer($this->createContainer());

        $response = $controller->index($this->createKeycloak(), $this->createStub(UserRepository::class));
        $this->assertSame(200, $response->getStatusCode());
    }

    public function testIndexWithUsers(): void
    {
        $users = [
            ['id' => 'kc-1', 'email' => 'a@test.com', 'firstName' => 'A', 'lastName' => 'B', 'enabled' => true, 'emailVerified' => true, 'createdTimestamp' => 1000],
        ];

        $userRepo = $this->createStub(UserRepository::class);
        $userRepo->method('findOneBy')->willReturn(null);

        $controller = new MembresController($this->createStub(LoggerInterface::class));
        $controller->setContainer($this->createContainer());

        $response = $controller->index($this->createKeycloak($users, [['name' => 'ecosplay_member']]), $userRepo);
        $this->assertSame(200, $response->getStatusCode());
    }

    public function testIndexWithLocalUser(): void
    {
        $users = [['id' => 'kc-2', 'email' => 'local@test.com', 'firstName' => 'L', 'lastName' => 'U', 'enabled' => true, 'emailVerified' => false, 'createdTimestamp' => 2000]];

        $localUser = new User();
        $localUser->setEmail('local@test.com');
        $localUser->setFirstName('L');
        $localUser->setLastName('U');
        $localUser->setPassword('h');

        $userRepo = $this->createStub(UserRepository::class);
        $userRepo->method('findOneBy')->willReturn($localUser);

        $controller = new MembresController($this->createStub(LoggerInterface::class));
        $controller->setContainer($this->createContainer());

        $response = $controller->index($this->createKeycloak($users), $userRepo);
        $this->assertSame(200, $response->getStatusCode());
    }

    public function testIndexWithCreatedGroups(): void
    {
        $kc = $this->createStub(KeycloakAdminService::class);
        $kc->method('ensureRequiredGroups')->willReturn(['ecosplay_member', 'ecosplay_admin']);
        $kc->method('listUsers')->willReturn([]);
        $kc->method('listGroups')->willReturn([]);

        $controller = new MembresController($this->createStub(LoggerInterface::class));
        $controller->setContainer($this->createContainer());

        $response = $controller->index($kc, $this->createStub(UserRepository::class));
        $this->assertSame(200, $response->getStatusCode());
    }

    public function testIndexKeycloakError(): void
    {
        $kc = $this->createStub(KeycloakAdminService::class);
        $kc->method('ensureRequiredGroups')->willThrowException(new \RuntimeException('KC down'));

        $controller = new MembresController($this->createStub(LoggerInterface::class));
        $controller->setContainer($this->createContainer());

        $response = $controller->index($kc, $this->createStub(UserRepository::class));
        $this->assertSame(200, $response->getStatusCode());
    }

    public function testIndexGetGroupsError(): void
    {
        $users = [['id' => 'kc-3', 'email' => 'err@test.com', 'firstName' => 'E', 'lastName' => 'R']];

        $kc = $this->createStub(KeycloakAdminService::class);
        $kc->method('ensureRequiredGroups')->willReturn([]);
        $kc->method('listUsers')->willReturn($users);
        $kc->method('getUserGroups')->willThrowException(new \RuntimeException('Groups error'));
        $kc->method('listGroups')->willReturn([]);

        $controller = new MembresController($this->createStub(LoggerInterface::class));
        $controller->setContainer($this->createContainer());

        $response = $controller->index($kc, $this->createStub(UserRepository::class));
        $this->assertSame(200, $response->getStatusCode());
    }

    public function testIndexListGroupsError(): void
    {
        $kc = $this->createStub(KeycloakAdminService::class);
        $kc->method('ensureRequiredGroups')->willReturn([]);
        $kc->method('listUsers')->willReturn([]);
        $kc->method('listGroups')->willThrowException(new \RuntimeException('Groups list error'));

        $controller = new MembresController($this->createStub(LoggerInterface::class));
        $controller->setContainer($this->createContainer());

        $response = $controller->index($kc, $this->createStub(UserRepository::class));
        $this->assertSame(200, $response->getStatusCode());
    }

    public function testCreateEmptyFields(): void
    {
        $request = new Request([], ['firstName' => '', 'lastName' => '', 'email' => '', 'groups' => []]);
        $request->setMethod('POST');

        $controller = new MembresController($this->createStub(LoggerInterface::class));
        $controller->setContainer($this->createContainer());

        $response = $controller->create($request, $this->createStub(KeycloakAdminService::class), $this->createStub(UserRepository::class), $this->createStub(EntityManagerInterface::class), $this->createStub(UserPasswordHasherInterface::class), $this->createStub(MailerService::class), $this->createStub(Environment::class));
        $this->assertSame(302, $response->getStatusCode());
    }

    public function testCreateEmailExists(): void
    {
        $existingUser = new User();
        $existingUser->setEmail('exist@test.com');
        $existingUser->setFirstName('E');
        $existingUser->setLastName('X');
        $existingUser->setPassword('h');

        $userRepo = $this->createStub(UserRepository::class);
        $userRepo->method('findOneBy')->willReturn($existingUser);

        $request = new Request([], ['firstName' => 'A', 'lastName' => 'B', 'email' => 'exist@test.com', 'groups' => []]);
        $request->setMethod('POST');

        $controller = new MembresController($this->createStub(LoggerInterface::class));
        $controller->setContainer($this->createContainer());

        $response = $controller->create($request, $this->createStub(KeycloakAdminService::class), $userRepo, $this->createStub(EntityManagerInterface::class), $this->createStub(UserPasswordHasherInterface::class), $this->createStub(MailerService::class), $this->createStub(Environment::class));
        $this->assertSame(302, $response->getStatusCode());
    }

    public function testCreateSuccess(): void
    {
        $kc = $this->createStub(KeycloakAdminService::class);
        $kc->method('createUser')->willReturn(['created' => true, 'keycloakId' => 'kc-new', 'tempPassword' => 'tmp123']);

        $userRepo = $this->createStub(UserRepository::class);
        $userRepo->method('findOneBy')->willReturn(null);

        $hasher = $this->createStub(UserPasswordHasherInterface::class);
        $hasher->method('hashPassword')->willReturn('hashed');

        $twig = $this->createStub(Environment::class);
        $twig->method('render')->willReturn('<html>email</html>');

        $request = new Request([], ['firstName' => 'Jean', 'lastName' => 'Membre', 'email' => 'jean@test.com', 'groups' => ['ecosplay_member', 'esy-web']]);
        $request->setMethod('POST');

        $controller = new MembresController($this->createStub(LoggerInterface::class));
        $controller->setContainer($this->createContainer());

        $response = $controller->create($request, $kc, $userRepo, $this->createStub(EntityManagerInterface::class), $hasher, $this->createStub(MailerService::class), $twig);
        $this->assertSame(302, $response->getStatusCode());
    }

    public function testCreateSuccessAdmin(): void
    {
        $kc = $this->createStub(KeycloakAdminService::class);
        $kc->method('createUser')->willReturn(['created' => true, 'keycloakId' => 'kc-admin', 'tempPassword' => 'tmp456']);

        $userRepo = $this->createStub(UserRepository::class);
        $userRepo->method('findOneBy')->willReturn(null);

        $hasher = $this->createStub(UserPasswordHasherInterface::class);
        $hasher->method('hashPassword')->willReturn('hashed');

        $request = new Request([], ['firstName' => 'Admin', 'lastName' => 'User', 'email' => 'admin@test.com', 'groups' => ['ecosplay_admin']]);
        $request->setMethod('POST');

        $controller = new MembresController($this->createStub(LoggerInterface::class));
        $controller->setContainer($this->createContainer());

        $response = $controller->create($request, $kc, $userRepo, $this->createStub(EntityManagerInterface::class), $hasher, $this->createStub(MailerService::class), $this->createStub(Environment::class));
        $this->assertSame(302, $response->getStatusCode());
    }

    public function testCreateKeycloakFailed(): void
    {
        $kc = $this->createStub(KeycloakAdminService::class);
        $kc->method('createUser')->willReturn(['created' => false, 'keycloakId' => null, 'tempPassword' => null]);

        $userRepo = $this->createStub(UserRepository::class);
        $userRepo->method('findOneBy')->willReturn(null);

        $request = new Request([], ['firstName' => 'A', 'lastName' => 'B', 'email' => 'fail@test.com', 'groups' => []]);
        $request->setMethod('POST');

        $controller = new MembresController($this->createStub(LoggerInterface::class));
        $controller->setContainer($this->createContainer());

        $response = $controller->create($request, $kc, $userRepo, $this->createStub(EntityManagerInterface::class), $this->createStub(UserPasswordHasherInterface::class), $this->createStub(MailerService::class), $this->createStub(Environment::class));
        $this->assertSame(302, $response->getStatusCode());
    }

    public function testCreateThrowable(): void
    {
        $kc = $this->createStub(KeycloakAdminService::class);
        $kc->method('createUser')->willThrowException(new \RuntimeException('KC error'));

        $userRepo = $this->createStub(UserRepository::class);
        $userRepo->method('findOneBy')->willReturn(null);

        $request = new Request([], ['firstName' => 'A', 'lastName' => 'B', 'email' => 'err@test.com', 'groups' => []]);
        $request->setMethod('POST');

        $controller = new MembresController($this->createStub(LoggerInterface::class));
        $controller->setContainer($this->createContainer());

        $response = $controller->create($request, $kc, $userRepo, $this->createStub(EntityManagerInterface::class), $this->createStub(UserPasswordHasherInterface::class), $this->createStub(MailerService::class), $this->createStub(Environment::class));
        $this->assertSame(302, $response->getStatusCode());
    }

    public function testResendSuccess(): void
    {
        $user = new User();
        $user->setEmail('resend@test.com');
        $user->setFirstName('R');
        $user->setLastName('S');
        $user->setPassword('h');
        $user->setTempPassword('tmp789');

        $userRepo = $this->createStub(UserRepository::class);
        $userRepo->method('find')->willReturn($user);

        $twig = $this->createStub(Environment::class);
        $twig->method('render')->willReturn('<html>resend</html>');

        $controller = new MembresController($this->createStub(LoggerInterface::class));
        $controller->setContainer($this->createContainer());

        $response = $controller->resend(1, $userRepo, $this->createStub(MailerService::class), $twig);
        $this->assertSame(302, $response->getStatusCode());
    }

    public function testResendUserNotFound(): void
    {
        $userRepo = $this->createStub(UserRepository::class);
        $userRepo->method('find')->willReturn(null);

        $controller = new MembresController($this->createStub(LoggerInterface::class));
        $controller->setContainer($this->createContainer());

        $response = $controller->resend(999, $userRepo, $this->createStub(MailerService::class), $this->createStub(Environment::class));
        $this->assertSame(302, $response->getStatusCode());
    }

    public function testResendNoTempPassword(): void
    {
        $user = new User();
        $user->setEmail('no-tmp@test.com');
        $user->setFirstName('N');
        $user->setLastName('T');
        $user->setPassword('h');

        $userRepo = $this->createStub(UserRepository::class);
        $userRepo->method('find')->willReturn($user);

        $controller = new MembresController($this->createStub(LoggerInterface::class));
        $controller->setContainer($this->createContainer());

        $response = $controller->resend(1, $userRepo, $this->createStub(MailerService::class), $this->createStub(Environment::class));
        $this->assertSame(302, $response->getStatusCode());
    }

    public function testDeleteSuccess(): void
    {
        $localUser = new User();
        $localUser->setEmail('del@test.com');
        $localUser->setFirstName('D');
        $localUser->setLastName('E');
        $localUser->setPassword('h');

        $userRepo = $this->createStub(UserRepository::class);
        $userRepo->method('findOneBy')->willReturn($localUser);

        $controller = new MembresController($this->createStub(LoggerInterface::class));
        $controller->setContainer($this->createContainer());

        $response = $controller->delete('kc-del', $this->createStub(KeycloakAdminService::class), $userRepo, $this->createStub(EntityManagerInterface::class));
        $this->assertSame(302, $response->getStatusCode());
    }

    public function testDeleteNoLocalUser(): void
    {
        $userRepo = $this->createStub(UserRepository::class);
        $userRepo->method('findOneBy')->willReturn(null);

        $controller = new MembresController($this->createStub(LoggerInterface::class));
        $controller->setContainer($this->createContainer());

        $response = $controller->delete('kc-nope', $this->createStub(KeycloakAdminService::class), $userRepo, $this->createStub(EntityManagerInterface::class));
        $this->assertSame(302, $response->getStatusCode());
    }

    public function testDeleteError(): void
    {
        $kc = $this->createStub(KeycloakAdminService::class);
        $kc->method('deleteUser')->willThrowException(new \RuntimeException('KC delete error'));

        $controller = new MembresController($this->createStub(LoggerInterface::class));
        $controller->setContainer($this->createContainer());

        $response = $controller->delete('kc-err', $kc, $this->createStub(UserRepository::class), $this->createStub(EntityManagerInterface::class));
        $this->assertSame(302, $response->getStatusCode());
    }
}
