package de.votian.services.controller;

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

    private ReportWorkspaceService reportWorkspaceService;
    private AccessControlService accessControlService;
    private ReportWorkspaceController controller;
    private Authentication authentication;
    private UserSessionDto user;

    @BeforeEach
    void setUp() {
        reportWorkspaceService = mock(ReportWorkspaceService.class);
        accessControlService = mock(AccessControlService.class);
        controller = new ReportWorkspaceController(reportWorkspaceService, accessControlService);
        authentication = mock(Authentication.class);
        user = new UserSessionDto();
        user.setUserType(1);
        user.setHeadquartersId(1L);
        user.setUserId(5L);
        user.setEmployeeId(23L);

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

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

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

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

    @Test
    void bootstrapDelegatesForAuthorizedUsers() {
        ReportWorkspaceService.Bootstrap bootstrap = new ReportWorkspaceService.Bootstrap(
                List.of(new ReportWorkspaceService.ObjectTypeOption("cs", "Kunden")),
                true
        );
        when(reportWorkspaceService.bootstrap(user, 1L)).thenReturn(bootstrap);

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

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

    @Test
    void createReportRejectsUnauthorizedUsers() {
        ReportWorkspaceController.ReportRequest request = new ReportWorkspaceController.ReportRequest();
        request.hqId = 1L;
        request.objectType = "cs";
        request.objectId = 44L;
        request.text = "Probe";
        when(accessControlService.canViewReports(user)).thenReturn(false);

        ResponseEntity<ReportWorkspaceService.ReportRow> response = controller.createReport(request, authentication);

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