package com.educational.platform.course.enrollments.query;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.UUID;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
import org.springframework.security.test.context.support.WithMockUser;

import com.educational.platform.course.enrollments.CompletionStatusDTO;
import com.educational.platform.course.enrollments.course.EnrollCourse;
import com.educational.platform.course.enrollments.course.EnrollCourseRepository;
import com.educational.platform.course.enrollments.student.Student;
import com.educational.platform.course.enrollments.student.StudentRepository;
import com.educational.platform.course.enrollments.course.create.CreateCourseCommand;
import com.educational.platform.course.enrollments.register.RegisterStudentToCourseCommand;
import com.educational.platform.course.enrollments.register.RegisterStudentToCourseCommandHandler;
import com.educational.platform.course.enrollments.student.create.CreateStudentCommand;

@AutoConfigureTestDatabase
@SpringBootTest
public class ListCourseEnrollmentsQueryHandlerIntegrationTest {

	public static final String STUDENT = "student";
	private final UUID courseUuid = UUID.fromString("123e4567-e89b-12d3-a456-426655440001");

	@Autowired
	private EnrollCourseRepository courseRepository;

	@Autowired
	private StudentRepository studentRepository;

	@Autowired
	private RegisterStudentToCourseCommandHandler registerStudentToCourseCommandHandler;

	@MockitoSpyBean
	private ListCourseEnrollmentsQueryHandler sut;

	@BeforeEach
	void setUp() {
		var course = new EnrollCourse(new CreateCourseCommand(courseUuid));
		courseRepository.save(course);

		var student = new Student(new CreateStudentCommand("student"));
		studentRepository.save(student);
	}

	@Test
	@WithMockUser(username = STUDENT, roles = "STUDENT")
	void handle_validQuery_courseEnrollmentRetrieved() {
		// given
		var command = RegisterStudentToCourseCommand.builder().courseId(courseUuid).build();
		var identifier = registerStudentToCourseCommandHandler.handle(command);

		// when
		var result = sut.handle(new ListCourseEnrollmentsQuery());

		// then
		assertThat(result).isNotEmpty();
		var dto = result.get(0);
		assertThat(dto)
				.hasFieldOrPropertyWithValue("uuid", identifier)
				.hasFieldOrPropertyWithValue("course", courseUuid)
				.hasFieldOrPropertyWithValue("student", STUDENT)
				.hasFieldOrPropertyWithValue("completionStatus", CompletionStatusDTO.IN_PROGRESS);
	}
}
