package ru.ulstu.is.cbapp;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import ru.ulstu.is.cbapp.models.DrivingSchool;
import ru.ulstu.is.cbapp.models.Student;
import ru.ulstu.is.cbapp.service.DrivingSchoolService;
import ru.ulstu.is.cbapp.service.StudentService;

@SpringBootTest
public class StudentServiceTests {
    @Autowired
    private StudentService studentService;

    @Autowired
    private DrivingSchoolService drivingSchoolService;

    @Test
    public void testAddStudent() {
        studentService.deleteAllStudents();

        studentService.addStudent("name", "surname", "111");
        Assertions.assertEquals(1, studentService.findAllStudents().size());
        studentService.deleteAllStudents();
    }

    @Test
    public void testUpdateStudent() {
        studentService.deleteAllStudents();
        final String newName = "name";

        Student e  = studentService.addStudent("surname", "name", "111");
        studentService.updateStudent(e.getId(),e.getSurname(), newName, e.getPhoneNumber());
        Assertions.assertEquals(newName, studentService.findStudent(e.getId()).getName());
        studentService.deleteAllStudents();
    }

    @Test
    public void testDeleteStudent() {
        studentService.deleteAllStudents();

        Student e = studentService.addStudent("name", "surname", "111");
        studentService.deleteStudent(e.getId());
        Assertions.assertEquals(0, studentService.findAllStudents().size());
        studentService.deleteAllStudents();
    }

    @Test
    public void testFindAllStudent() {
        studentService.deleteAllStudents();
        int n = 3;
        for (int i = 0; i < n; i++) {
            studentService.addStudent("name", "surname", "111");
        }
        Assertions.assertEquals(n, studentService.findAllStudents().size());
        studentService.deleteAllStudents();
    }

    @Test
    public void testAddDrivingSchool() {
        studentService.deleteAllStudents();
        drivingSchoolService.deleteAllDrivingSchools();

        Student e = studentService.addStudent("name", "surname", "111");
        DrivingSchool d = drivingSchoolService.addDrivingSchool("Comp");
        drivingSchoolService.addNewStudent(d.getId(), e);
        Assertions.assertEquals(d, studentService.findStudent(e.getId()).getDrivingSchool());

        studentService.deleteAllStudents();
        drivingSchoolService.deleteAllDrivingSchools();
    }

    @Test
    public void testDeleteFromDrivingSchool() {
        studentService.deleteAllStudents();
        drivingSchoolService.deleteAllDrivingSchools();

        Student e = studentService.addStudent("name", "surname", "111");
        DrivingSchool d = drivingSchoolService.addDrivingSchool("Comp");

        studentService.addDrivingSchool(e.getId(), d);
        Assertions.assertEquals(d, studentService.findStudent(e.getId()).getDrivingSchool());

        studentService.deleteDrivingSchool(e.getId());
        Assertions.assertNull(studentService.findStudent(e.getId()).getDrivingSchool());

        studentService.deleteAllStudents();
        drivingSchoolService.deleteAllDrivingSchools();
    }
}
