package de.votian.services.controller;

import de.votian.services.dto.GroupAdminDto;
import de.votian.services.dto.UserSessionDto;
import de.votian.services.security.AccessControlService;
import de.votian.services.service.GroupFilterAdminService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;

import java.util.List;
import java.util.Objects;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class GroupFilterAdminControllerTest {

    private GroupFilterAdminService groupFilterAdminService;
    private AccessControlService accessControlService;
    private GroupFilterAdminController controller;
    private Authentication authentication;
    private UserSessionDto user;

    @BeforeEach
    void setUp() {
        groupFilterAdminService = mock(GroupFilterAdminService.class);
        accessControlService = mock(AccessControlService.class);
        controller = new GroupFilterAdminController(groupFilterAdminService, accessControlService);
        authentication = mock(Authentication.class);
        user = new UserSessionDto();
        user.setUserType(1);
        user.setHeadquartersId(1L);
        user.setEmployeeId(23L);

        when(accessControlService.sessionUser(authentication)).thenReturn(user);
        when(accessControlService.isHeadquartersUser(user)).thenReturn(true);
        when(accessControlService.canManageHeadquarters(user)).thenReturn(true);
        when(accessControlService.canAccessHeadquarters(user, 1L)).thenReturn(true);
    }

    @Test
    void getGroupsRejectsUnauthorizedUsers() {
        when(accessControlService.isHeadquartersUser(user)).thenReturn(false);

        ResponseEntity<List<GroupAdminDto>> response = controller.getGroups(1L, authentication);

        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
    }

    @Test
    void getGroupsDelegatesForAuthorizedHeadquartersUser() {
        GroupAdminDto dto = new GroupAdminDto();
        dto.setId(5L);
        dto.setName("Dispo");
        when(groupFilterAdminService.findGroups(1L)).thenReturn(List.of(dto));

        ResponseEntity<List<GroupAdminDto>> response = controller.getGroups(1L, authentication);

        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
        assertThat(response.getBody()).isNotNull().hasSize(1);
        assertThat(Objects.requireNonNull(response.getBody()).get(0).getName()).isEqualTo("Dispo");
        verify(groupFilterAdminService).findGroups(1L);
    }

    @Test
    void createGroupRejectsUnauthorizedUsers() {
        GroupFilterAdminController.GroupRequest request = new GroupFilterAdminController.GroupRequest();
        request.hqId = 1L;
        request.name = "Dispo";
        when(accessControlService.canManageHeadquarters(user)).thenReturn(false);

        ResponseEntity<GroupAdminDto> response = controller.createGroup(request, authentication);

        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
    }
}
