package de.votian.services.controller;

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

    private GenericExportWorkspaceService genericExportWorkspaceService;
    private AccessControlService accessControlService;
    private GenericExportWorkspaceController controller;
    private Authentication authentication;
    private UserSessionDto user;

    @BeforeEach
    void setUp() {
        genericExportWorkspaceService = mock(GenericExportWorkspaceService.class);
        accessControlService = mock(AccessControlService.class);
        controller = new GenericExportWorkspaceController(genericExportWorkspaceService, accessControlService);
        authentication = mock(Authentication.class);
        user = new UserSessionDto();
        user.setUserType(1);
        user.setHeadquartersId(1L);
        user.setUserId(7L);

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

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

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

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

    @Test
    void bootstrapDelegatesForAuthorizedUsers() {
        GenericExportWorkspaceService.Bootstrap bootstrap = new GenericExportWorkspaceService.Bootstrap(
                List.of(new GenericExportWorkspaceService.CategoryOption(1L, "Kunden", "1")),
                false,
                0L
        );
        when(genericExportWorkspaceService.bootstrap(user, 1L)).thenReturn(bootstrap);

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

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

    @Test
    void runExportRejectsUnauthorizedUsers() {
        GenericExportWorkspaceController.ExportRequest request = new GenericExportWorkspaceController.ExportRequest();
        request.hqId = 1L;
        request.categoryId = 1L;
        when(accessControlService.canViewGenericExports(user)).thenReturn(false);

        ResponseEntity<GenericExportWorkspaceService.ExportResult> response = controller.runExport(request, authentication);

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