package at.ac.tuwien.inso.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

import at.ac.tuwien.inso.entity.Lecturer;
import at.ac.tuwien.inso.entity.Subject;
import at.ac.tuwien.inso.exception.BusinessObjectNotFoundException;
import at.ac.tuwien.inso.exception.SubjectNotFoundException;
import at.ac.tuwien.inso.repository.LecturerRepository;
import at.ac.tuwien.inso.repository.SubjectRepository;
import at.ac.tuwien.inso.service.SubjectService;


@Service
public class SubjectServiceImpl implements SubjectService {

    private static final Logger logger = LoggerFactory.getLogger(SubjectServiceImpl.class);

    @Autowired
    private SubjectRepository subjectRepository;

    @Autowired
    private LecturerRepository lecturerRepository;

    @Override
    @Transactional(readOnly = true)
    public Page<Subject> findAll(Pageable pageable) {
        return subjectRepository.findAll(pageable);
    }

    @Override
    @Transactional(readOnly = true)
    public Subject findOne(long id) {
        return subjectRepository.findById(id);
    }

    @Override
    @Transactional
    public Subject create(Subject subject) {
        return subjectRepository.save(subject);
    }

    @Override
    @Transactional
    public Subject addLecturerToSubject(Long subjectId, Long lecturerUisUserId) {
        logger.info("addLecturerToSubject for subject {} and lecturer {}", subjectId,
                lecturerUisUserId);

        Lecturer lecturer = lecturerRepository.findById(lecturerUisUserId);

        if (lecturer == null) {
            String msg = "Lecturer with user id " + lecturerUisUserId + " not found";
            logger.info(msg);
            throw new BusinessObjectNotFoundException(msg);
        }

        Subject subject = subjectRepository.findById(subjectId);

        if (subject == null) {
            String msg = "Subject with id " + lecturerUisUserId + " not found";
            logger.info(msg);
            throw new SubjectNotFoundException(msg);
        }

        subject.addLecturers(lecturer);

        Subject savedSubject = subjectRepository.save(subject);

        return savedSubject;
    }


    @Override
    @Transactional(readOnly = true)
    public List<Lecturer> getAvailableLecturersForSubject(Long subjectId) {
        Subject subject = subjectRepository.findById(subjectId);

        if (subject == null) {
            throw new SubjectNotFoundException("Subject with id '" + subjectId + "' not found");
        }

        List<Lecturer> currentLecturers = subject.getLecturers();

        List<Lecturer> allLecturers = lecturerRepository.findAll();

        return allLecturers
                .stream()
                .filter(lecturer -> !currentLecturers.contains(lecturer))
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(readOnly = true)
    public List<Subject> searchForSubjects(String word) {
        return subjectRepository.findByNameContainingIgnoreCase(word);
    }
}

@@ -564,32 +564,156 @@
 FoundException;%0A
+import at.ac.tuwien.inso.exception.LecturerNotFoundException;%0Aimport at.ac.tuwien.inso.exception.RelationNotfoundException;%0A
 import at.ac.tuw
@@ -3241,32 +3241,1314 @@
 List());%0A    %7D%0A%0A
+    @Override%0A    public Lecturer removeLecturerFromSubject(Long subjectId, Long lecturerUisUserId) %7B%0A        logger.info(%22removeLecturerFromSubject for subject %7B%7D and lecturer %7B%7D%22, subjectId,%0A                lecturerUisUserId);%0A%0A        Subject subject = subjectRepository.findById(subjectId);%0A%0A        if (subject == null) %7B%0A            String msg = %22Subject with id '%22 + subjectId + %22' not found%22;%0A            logger.info(msg);%0A            throw new SubjectNotFoundException(msg);%0A        %7D%0A%0A        Lecturer lecturer = lecturerRepository.findById(lecturerUisUserId);%0A%0A        if (lecturer == null) %7B%0A            String msg = %22Lecturer with id '%22 + lecturerUisUserId + %22' not found%22;%0A            logger.info(msg);%0A            throw new LecturerNotFoundException(msg);%0A        %7D%0A%0A        List%3CLecturer%3E currentLecturers = subject.getLecturers();%0A%0A        boolean isLecturer = currentLecturers.contains(lecturer);%0A%0A        if (!isLecturer) %7B%0A            String msg = %22Lecturer with id %22 + lecturerUisUserId + %22 not found for subject %22 +%0A                    subjectId;%0A            logger.info(msg);%0A            throw new RelationNotfoundException(msg);%0A        %7D%0A%0A        subject.removeLecturers(lecturer);%0A%0A        subjectRepository.save(subject);%0A%0A        return lecturer;%0A    %7D%0A%0A
     @Override%0A  

Implement SubjectServiceImple.removeLecturerFromSubject