package com.example.projectback.controllers;

import com.example.projectback.model.*;
import com.example.projectback.repository.GradeRepository;
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.LinkedHashMap;
import java.util.List;

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

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

    @GetMapping
    public List<Student> getAllStudents() {
        return studentRepository.findAll();
    }

    @PostMapping
    public ResponseEntity<Student> addStudent(@RequestBody Student student) {
        studentRepository.save(student);
        return new ResponseEntity<>(student, HttpStatus.CREATED);
    }

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

    @PostMapping("/{id}/grades")
    public ResponseEntity<Grade> addGrade(@PathVariable("id") Long id, @RequestBody LinkedHashMap<String, Object> grade) {
        Student student = this.studentRepository.getById(id);
        Grade newGrade = new Grade();
        Integer markRequest = (Integer) grade.get("mark");
        newGrade.setMark(markRequest.longValue());
        newGrade.setDescription((String) grade.get("description"));
        LinkedHashMap<String, Object> subjectRequest = (LinkedHashMap<String, Object>) grade.get("subject");
        Integer subjectIDRequest = (Integer) subjectRequest.get("id");
        Subject subject = this.subjectRepository.getById(Long.valueOf(subjectIDRequest));
        newGrade.setSubject(subject);

        student.getGrades().add(newGrade);
        this.gradeRepository.save(newGrade);
        this.studentRepository.save(student);
        return new ResponseEntity<>(newGrade, HttpStatus.OK);
    }

    @DeleteMapping("/{studentID}/grades/{gradeID}")
    public ResponseEntity<Student> deleteGrade(@PathVariable("studentID") Long studentID, @PathVariable("gradeID") Long gradeID) {
        Student student = this.studentRepository.getById(studentID);
        Grade grade = this.gradeRepository.getById(gradeID);
        student.getGrades().remove(grade);
        this.studentRepository.save(student);
        this.gradeRepository.delete(grade);
        return new ResponseEntity<>(student, HttpStatus.OK);
    }

    @PatchMapping("/{studentID}/payment/{subjectID}")
    public ResponseEntity<Student> confirmPayment(@PathVariable("studentID") Long studentID, @PathVariable("subjectID") Long subjectID, @RequestBody SubjectStudent subjectStudentReq) {
        Student student = this.studentRepository.getById(studentID);
        Subject subject = this.subjectRepository.getById(subjectID);
        SubjectStudent subjectStudent = this.subjectStudentRepository.getByStudentAndSubject(student, subject);
        subjectStudent.setPaymentConfirmed(subjectStudentReq.isPaymentConfirmed());
        this.subjectStudentRepository.save(subjectStudent);
        return new ResponseEntity<>(student, HttpStatus.OK);
    }
}
