package de.votian.services.controller;

import de.votian.services.dto.UserSessionDto;
import de.votian.services.security.AccessControlService;
import de.votian.services.service.GeoOperationsWorkspaceService;
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 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 GeoOperationsWorkspaceControllerTest {

    private GeoOperationsWorkspaceService geoOperationsWorkspaceService;
    private AccessControlService accessControlService;
    private GeoOperationsWorkspaceController controller;
    private Authentication authentication;
    private UserSessionDto user;

    @BeforeEach
    void setUp() {
        geoOperationsWorkspaceService = mock(GeoOperationsWorkspaceService.class);
        accessControlService = mock(AccessControlService.class);
        controller = new GeoOperationsWorkspaceController(geoOperationsWorkspaceService, accessControlService);
        authentication = mock(Authentication.class);
        user = new UserSessionDto();
        user.setUserType(1);
        user.setHeadquartersId(1L);
        user.setUserId(7L);
        user.setEmployeeId(11L);

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

    @Test
    void bootstrapRejectsUnauthorizedUsers() {
        when(accessControlService.canViewGeoOperations(user)).thenReturn(false);

        ResponseEntity<GeoOperationsWorkspaceService.Bootstrap> response = controller.bootstrap(1L, authentication);

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

    @Test
    void bootstrapDelegatesForAuthorizedUsers() {
        GeoOperationsWorkspaceService.Bootstrap bootstrap = new GeoOperationsWorkspaceService.Bootstrap(
                true,
                true,
                true,
                true,
                true,
                "Berlin",
                false,
                30
        );
        when(geoOperationsWorkspaceService.bootstrap(user, 1L)).thenReturn(bootstrap);

        ResponseEntity<GeoOperationsWorkspaceService.Bootstrap> response = controller.bootstrap(1L, authentication);

        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
        assertThat(response.getBody()).isEqualTo(bootstrap);
        verify(geoOperationsWorkspaceService).bootstrap(user, 1L);
    }

    @Test
    void mapRejectsUsersWithoutMapRight() {
        when(accessControlService.canViewGlobalMap(user)).thenReturn(false);

        ResponseEntity<GeoOperationsWorkspaceService.MapDashboard> response = controller.map(1L, authentication);

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