package com.example.projectback.controllers;

import com.example.projectback.model.Changeset;
import com.example.projectback.model.Student;
import com.example.projectback.model.Subject;
import com.example.projectback.model.SubjectStudent;
import com.example.projectback.repository.StudentRepository;
import com.example.projectback.repository.SubjectRepository;
import com.example.projectback.repository.SubjectStudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@CrossOrigin(origins = "http://localhost:4200")
@RequestMapping("/subjects")
public class SubjectController {
    private final SubjectRepository subjectRepository;
    private final SubjectStudentRepository subjectStudentRepository;
    private final StudentRepository studentRepository;

    @Autowired
    public SubjectController(SubjectRepository subjectRepository, SubjectStudentRepository subjectStudentRepository, StudentRepository studentRepository) {
        this.subjectRepository = subjectRepository;
        this.subjectStudentRepository = subjectStudentRepository;
        this.studentRepository = studentRepository;
    }

    @GetMapping
    public List<Subject> getAllSubjects() {return subjectRepository.findAll();}

    @GetMapping("/{id}")
    public ResponseEntity<Subject> getSubject(@PathVariable("id") Long id) {
        Optional<Subject> subject = subjectRepository.findById(id);
        if (subject.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(subject.get(), HttpStatus.OK);
    }

    @PostMapping
    public ResponseEntity<Subject> addSubject(@RequestBody Subject subject) {
        subjectRepository.save(subject);
        return new ResponseEntity<>(subject, HttpStatus.CREATED);
    }

    @PostMapping("/delete")
    public ResponseEntity<Changeset> deleteSubjects(@RequestBody Changeset changeset) {
        for (Long recId : changeset.getRecords()) {
            subjectRepository.deleteById(recId);
        }
        return new ResponseEntity<>(changeset, HttpStatus.OK);
    }

    @PatchMapping("/{id}")
    public ResponseEntity<Subject> updatePartOfSubject(@RequestBody Map<String, Object> updates, @PathVariable("id") Long id) {
        Subject subject = subjectRepository.getById(id);
        if (subject == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        partialUpdates(subject, updates);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    private void partialUpdates(Subject subject, Map<String, Object> updates) {
        if (updates.containsKey("name")) {
            subject.setName((String) updates.get("nane"));
        }
        if (updates.containsKey("studentAssoc")) {
            ArrayList<SubjectStudent> newStudents = new ArrayList<>();
            for (Integer studentId : (ArrayList<Integer>)updates.get("studentAssoc")) {
                Student student = this.studentRepository.getById(studentId.longValue());
                SubjectStudent newAssoc = new SubjectStudent(subject, student);
                this.subjectStudentRepository.save(newAssoc);
                newStudents.add(newAssoc);
            }
            subject.setStudentsAssoc(newStudents);
        }
        subjectRepository.save(subject);
    }
}
