package com.magistr.app.controller;

import com.magistr.app.dto.UserResponse;
import com.magistr.app.model.Department;
import com.magistr.app.model.LifecycleEntity;
import com.magistr.app.model.Role;
import com.magistr.app.model.User;
import com.magistr.app.repository.DepartmentRepository;
import com.magistr.app.repository.TeacherDepartmentAssignmentRepository;
import com.magistr.app.repository.UserRepository;
import com.magistr.app.service.TeacherDepartmentService;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Optional;

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

class UserControllerTest {

    @Test
    void teachersUseUnifiedUserResponseMapping() {
        UserRepository userRepository = mock(UserRepository.class);
        DepartmentRepository departmentRepository = mock(DepartmentRepository.class);
        TeacherDepartmentAssignmentRepository assignmentRepository = mock(TeacherDepartmentAssignmentRepository.class);
        TeacherDepartmentService teacherDepartmentService = mock(TeacherDepartmentService.class);
        UserController controller = new UserController(
                userRepository,
                null,
                departmentRepository,
                assignmentRepository,
                teacherDepartmentService
        );
        User teacher = user(10L, "teacher", Role.TEACHER, 2L);
        when(teacherDepartmentService.findTeachersForDepartmentAtDate(
                org.mockito.ArgumentMatchers.eq(2L),
                org.mockito.ArgumentMatchers.any()
        ))
                .thenReturn(List.of(teacher));
        when(departmentRepository.findById(2L))
                .thenReturn(Optional.of(new Department(2L, "Кафедра ВТ", 2L)));

        var response = controller.getTeachersByDepartmentId(2L);

        assertThat(response.getStatusCode().value()).isEqualTo(200);
        @SuppressWarnings("unchecked")
        List<UserResponse> body = (List<UserResponse>) response.getBody();
        assertThat(body).hasSize(1);
        assertThat(body.get(0).getUsername()).isEqualTo("teacher");
        assertThat(body.get(0).getDepartmentId()).isEqualTo(2L);
        assertThat(body.get(0).getDepartmentName()).isEqualTo("Кафедра ВТ");
        assertThat(body.get(0).getStatus()).isEqualTo(LifecycleEntity.STATUS_ACTIVE);
    }

    private User user(Long id, String username, Role role, Long departmentId) {
        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setRole(role);
        user.setFullName("Тестовый пользователь");
        user.setJobTitle("Преподаватель");
        user.setDepartmentId(departmentId);
        return user;
    }
}
