package de.effigenix.application.usermanagement;

import de.effigenix.application.usermanagement.dto.UserDTO;
import de.effigenix.domain.usermanagement.*;
import de.effigenix.shared.common.Result;
import de.effigenix.shared.security.ActorId;
import de.effigenix.shared.security.AuthorizationPort;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.HashSet;
import java.util.Optional;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
@DisplayName("GetUser Use Case")
class GetUserTest {

    @Mock private UserRepository userRepository;
    @Mock private AuthorizationPort authPort;

    private GetUser getUser;
    private ActorId performedBy;
    private User testUser;

    @BeforeEach
    void setUp() {
        getUser = new GetUser(userRepository, authPort);
        performedBy = ActorId.of("admin-user");
        testUser = User.reconstitute(
                UserId.of("user-1"), "john.doe", "john@example.com",
                new PasswordHash("$2a$12$R9h/cIPz0gi.URNN3kh2OPST9EBwVeL00lzQRYe3z08MZx3e8YCWW"),
                new HashSet<>(), "branch-1", UserStatus.ACTIVE, OffsetDateTime.now(ZoneOffset.UTC), null
        );
    }

    @Test
    @DisplayName("should_ReturnUser_When_UserExistsAndAuthorized")
    void should_ReturnUser_When_UserExistsAndAuthorized() {
        when(authPort.can(performedBy, UserManagementAction.USER_VIEW)).thenReturn(true);
        when(userRepository.findById(UserId.of("user-1"))).thenReturn(Result.success(Optional.of(testUser)));

        Result<UserError, UserDTO> result = getUser.execute("user-1", performedBy);

        assertThat(result.isSuccess()).isTrue();
        assertThat(result.unsafeGetValue().username()).isEqualTo("john.doe");
        assertThat(result.unsafeGetValue().email()).isEqualTo("john@example.com");
        assertThat(result.unsafeGetValue().id()).isEqualTo("user-1");
    }

    @Test
    @DisplayName("should_FailWithUnauthorized_When_ActorLacksPermission")
    void should_FailWithUnauthorized_When_ActorLacksPermission() {
        when(authPort.can(performedBy, UserManagementAction.USER_VIEW)).thenReturn(false);

        Result<UserError, UserDTO> result = getUser.execute("user-1", performedBy);

        assertThat(result.isFailure()).isTrue();
        assertThat(result.unsafeGetError()).isInstanceOf(UserError.Unauthorized.class);
        verify(userRepository, never()).findById(any());
    }

    @Test
    @DisplayName("should_FailWithUserNotFound_When_UserDoesNotExist")
    void should_FailWithUserNotFound_When_UserDoesNotExist() {
        when(authPort.can(performedBy, UserManagementAction.USER_VIEW)).thenReturn(true);
        when(userRepository.findById(UserId.of("nonexistent"))).thenReturn(Result.success(Optional.empty()));

        Result<UserError, UserDTO> result = getUser.execute("nonexistent", performedBy);

        assertThat(result.isFailure()).isTrue();
        assertThat(result.unsafeGetError()).isInstanceOf(UserError.UserNotFound.class);
    }
}
