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

import at.ac.tuwien.inso.entity.*;
import at.ac.tuwien.inso.exception.BusinessObjectNotFoundException;
import at.ac.tuwien.inso.exception.SubjectNotFoundException;
import at.ac.tuwien.inso.repository.*;
import at.ac.tuwien.inso.service.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.*;
import org.springframework.transaction.annotation.*;

import java.util.*;

@Service
public class SubjectServiceImpl implements SubjectService {

    @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) {
        Lecturer lecturer = lecturerRepository.findById(lecturerUisUserId);

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

        Subject subject = subjectRepository.findById(subjectId);

        if (subject == null) {
            String msg = "Subject with id " + lecturerUisUserId + " not found";
            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);
    }


}

@@ -108,100 +108,9 @@
 ion.
-BusinessObjectNotFoundException;%0Aimport at.ac.tuwien.inso.exception.SubjectNotFoundException
+*
 ;%0Aim
@@ -280,61 +280,9 @@
 ain.
-Page;%0Aimport org.springframework.data.domain.Pageable
+*
 ;%0Aim
@@ -374,16 +374,16 @@
 ion.*;%0A%0A
-
 import j
@@ -393,16 +393,43 @@
 .util.*;
+%0Aimport java.util.stream.*;
 %0A%0A@Servi

fix compile error