package dev.ksan.etfoglasiserver.service;

import dev.ksan.etfoglasiserver.model.Subject;
import dev.ksan.etfoglasiserver.model.SubjectAlt;
import dev.ksan.etfoglasiserver.repository.SubjectAltRepo;
import java.util.List;
import java.util.UUID;

import dev.ksan.etfoglasiserver.repository.SubjectRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class SubjectAltService {
  @Autowired SubjectAltRepo subjectAltRepo;

  public List<SubjectAlt> getSubjectAlts() {
    return subjectAltRepo.findAll();
  }

  public SubjectAlt getSubjectAlt(UUID id) {
    return subjectAltRepo
        .findById(id)
        .orElseThrow(() -> new RuntimeException("subjectAlt not found"));
  }

  // todo
  public void addSubjectAlt(SubjectAlt subjectAlt) {

    try {

      subjectAltRepo.save(subjectAlt);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  // todo add update method and modify add

  public void deleteSubjectAlt(UUID id) {
    subjectAltRepo.deleteById(id);
  }

  public Subject findSubjectIdWithTitle(String title) {
    List<Subject> result =  subjectAltRepo.findSubjectIdWithTitle(title);

    if(result.isEmpty() ) {
      result = subjectAltRepo.findSubjectIdWithTitle("Not Grouped");
      System.out.println(title);
    }

    return result.get(0);
  }
}
