package de.votian.services.controller;

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

    private MetaFieldAdminService metaFieldAdminService;
    private AccessControlService accessControlService;
    private MetaFieldAdminController controller;
    private Authentication authentication;
    private UserSessionDto user;

    @BeforeEach
    void setUp() {
        metaFieldAdminService = mock(MetaFieldAdminService.class);
        accessControlService = mock(AccessControlService.class);
        controller = new MetaFieldAdminController(metaFieldAdminService, 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.canManageMetaFields(user)).thenReturn(true);
        when(accessControlService.canAccessHeadquarters(user, 1L)).thenReturn(true);
    }

    @Test
    void getCategoriesRejectsUnauthorizedUsers() {
        when(accessControlService.canManageMetaFields(user)).thenReturn(false);

        ResponseEntity<List<MetaFieldAdminService.CategoryRow>> response = controller.getCategories(1L, authentication);

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

    @Test
    void getCategoriesDelegatesForAuthorizedUsers() {
        MetaFieldAdminService.CategoryRow row = new MetaFieldAdminService.CategoryRow(9L, "RP", "Report", null, "", 3L);
        when(metaFieldAdminService.findCategories(1L)).thenReturn(List.of(row));

        ResponseEntity<List<MetaFieldAdminService.CategoryRow>> response = controller.getCategories(1L, authentication);

        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
        assertThat(response.getBody()).containsExactly(row);
        verify(metaFieldAdminService).findCategories(1L);
    }

    @Test
    void createCategoryRejectsUnauthorizedUsers() {
        MetaFieldAdminController.CategoryRequest request = new MetaFieldAdminController.CategoryRequest();
        request.hqId = 1L;
        request.mnemonic = "RP";
        request.description = "Report";
        when(accessControlService.canManageMetaFields(user)).thenReturn(false);

        ResponseEntity<MetaFieldAdminService.CategoryRow> response = controller.createCategory(request, authentication);

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