package com.magistr.app.controller;

import com.magistr.app.dto.AcademicCalendarDto;
import com.magistr.app.model.AcademicCalendar;
import com.magistr.app.model.AcademicYear;
import com.magistr.app.model.EducationForm;
import com.magistr.app.model.Speciality;
import com.magistr.app.model.SpecialtyProfile;
import com.magistr.app.repository.AcademicCalendarDayRepository;
import com.magistr.app.repository.AcademicCalendarRepository;
import com.magistr.app.repository.AcademicCalendarSubjectRepository;
import com.magistr.app.repository.AcademicYearRepository;
import com.magistr.app.repository.EducationFormRepository;
import com.magistr.app.repository.SpecialtiesRepository;
import com.magistr.app.repository.SpecialtyProfileRepository;
import com.magistr.app.repository.SubjectRepository;
import com.magistr.app.service.AcademicCalendarGridService;
import com.magistr.app.service.AcademicStructureService;
import com.magistr.app.service.ScheduleGeneratorService;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.util.Optional;

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

class AcademicCalendarControllerTest {

    @Test
    void createCalendarBuildsTitleWithoutClientValue() {
        AcademicCalendarRepository calendarRepository = mock(AcademicCalendarRepository.class);
        AcademicYearRepository academicYearRepository = mock(AcademicYearRepository.class);
        SpecialtiesRepository specialtiesRepository = mock(SpecialtiesRepository.class);
        SpecialtyProfileRepository profileRepository = mock(SpecialtyProfileRepository.class);
        EducationFormRepository educationFormRepository = mock(EducationFormRepository.class);

        AcademicYear year = new AcademicYear();
        year.setId(1L);
        year.setTitle("2026-2027");
        year.setStartDate(LocalDate.of(2026, 9, 1));
        year.setEndDate(LocalDate.of(2027, 6, 30));
        Speciality speciality = new Speciality(2L, "Программная инженерия", "09.03.04");
        SpecialtyProfile profile = new SpecialtyProfile();
        profile.setId(3L);
        profile.setSpeciality(speciality);
        profile.setName("Разработка ПО");
        EducationForm studyForm = new EducationForm();
        studyForm.setId(4L);
        studyForm.setName("Очная");

        when(academicYearRepository.findById(1L)).thenReturn(Optional.of(year));
        when(specialtiesRepository.findById(2L)).thenReturn(Optional.of(speciality));
        when(profileRepository.findById(3L)).thenReturn(Optional.of(profile));
        when(educationFormRepository.findById(4L)).thenReturn(Optional.of(studyForm));
        when(calendarRepository.save(any(AcademicCalendar.class)))
                .thenAnswer(invocation -> invocation.getArgument(0));

        AcademicCalendarController controller = new AcademicCalendarController(
                calendarRepository,
                mock(AcademicCalendarDayRepository.class),
                academicYearRepository,
                specialtiesRepository,
                profileRepository,
                educationFormRepository,
                mock(SubjectRepository.class),
                mock(AcademicCalendarSubjectRepository.class),
                mock(AcademicCalendarGridService.class),
                mock(ScheduleGeneratorService.class),
                mock(AcademicStructureService.class)
        );
        AcademicCalendarDto request = new AcademicCalendarDto(
                null,
                null,
                1L,
                null,
                null,
                null,
                2L,
                null,
                null,
                3L,
                null,
                4L,
                null,
                4
        );

        var response = controller.createCalendar(request);

        assertThat(response.getStatusCode().value()).isEqualTo(200);
        assertThat(response.getBody()).isInstanceOf(AcademicCalendarDto.class);
        assertThat(((AcademicCalendarDto) response.getBody()).title())
                .isEqualTo("09.03.04 — Разработка ПО — Очная — 2026-2027");
    }
}
