package com.magistr.app.controller;

import com.magistr.app.model.AcademicYear;
import com.magistr.app.model.Semester;
import com.magistr.app.model.SemesterType;
import com.magistr.app.repository.SemesterRepository;
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.Mockito.mock;
import static org.mockito.Mockito.when;

class ScheduleControllerTest {

    @Test
    void returnsSemestersForReadOnlyScheduleFilterInNewestFirstOrder() {
        ScheduleQueryService scheduleQueryService = mock(ScheduleQueryService.class);
        SemesterRepository semesterRepository = mock(SemesterRepository.class);
        ScheduleController controller = new ScheduleController(scheduleQueryService, semesterRepository);

        Semester autumn = semester(1L, SemesterType.autumn,
                LocalDate.of(2025, 9, 1), LocalDate.of(2026, 1, 31));
        Semester spring = semester(2L, SemesterType.spring,
                LocalDate.of(2026, 2, 9), LocalDate.of(2026, 6, 30));
        when(semesterRepository.findAll()).thenReturn(List.of(autumn, spring));

        var result = controller.getSemesters();

        assertThat(result).extracting(item -> item.id()).containsExactly(2L, 1L);
        assertThat(result.get(0).academicYearTitle()).isEqualTo("2025/2026");
        assertThat(result.get(0).semesterType()).isEqualTo(SemesterType.spring);
        assertThat(result.get(0).startDate()).isEqualTo(LocalDate.of(2026, 2, 9));
        assertThat(result.get(0).endDate()).isEqualTo(LocalDate.of(2026, 6, 30));
    }

    private Semester semester(Long id, SemesterType type, LocalDate startDate, LocalDate endDate) {
        AcademicYear academicYear = new AcademicYear();
        academicYear.setId(10L);
        academicYear.setTitle("2025/2026");
        academicYear.setStartDate(LocalDate.of(2025, 9, 1));
        academicYear.setEndDate(LocalDate.of(2026, 8, 31));

        Semester semester = new Semester();
        semester.setId(id);
        semester.setAcademicYear(academicYear);
        semester.setSemesterType(type);
        semester.setStartDate(startDate);
        semester.setEndDate(endDate);
        return semester;
    }
}
