package com.magistr.app.controller;

import com.magistr.app.model.Classroom;
import com.magistr.app.model.LifecycleEntity;
import com.magistr.app.repository.ClassroomRepository;
import com.magistr.app.repository.DepartmentRepository;
import com.magistr.app.repository.UserRepository;
import com.magistr.app.service.ScheduleQueryService;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.util.List;

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

class WorkloadControllerTest {

    @Test
    void freeClassroomsSkipsNullAvailabilityWithoutNpe() {
        ScheduleQueryService scheduleQueryService = mock(ScheduleQueryService.class);
        ClassroomRepository classroomRepository = mock(ClassroomRepository.class);
        WorkloadController controller = new WorkloadController(
                scheduleQueryService,
                classroomRepository,
                mock(UserRepository.class),
                mock(DepartmentRepository.class)
        );
        when(scheduleQueryService.search(any(), any(), any(), any(), any(), any(), eq(1L), any(), any(), any()))
                .thenReturn(List.of());
        when(classroomRepository.findByStatusNot(LifecycleEntity.STATUS_ARCHIVED))
                .thenReturn(List.of(classroom(1L, null), classroom(2L, false), classroom(3L, true)));

        var result = controller.freeClassrooms(LocalDate.of(2026, 5, 27), 1L);

        assertThat(result).extracting("id").containsExactly(3L);
    }

    private Classroom classroom(Long id, Boolean available) {
        Classroom classroom = new Classroom();
        classroom.setId(id);
        classroom.setName("Аудитория " + id);
        classroom.setCapacity(30);
        classroom.setIsAvailable(available);
        return classroom;
    }
}
