package ovh.herisson.Clyde.EndPoints;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
import ovh.herisson.Clyde.Services.AuthenticatorService;
import ovh.herisson.Clyde.Services.CourseService;
import ovh.herisson.Clyde.Services.ProtectionService;
import ovh.herisson.Clyde.Services.TeacherCourseService;
import ovh.herisson.Clyde.Tables.Course;
import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.User;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin(originPatterns = "*", allowCredentials = "true")
public class CourseController {

    private final CourseService courseServ;

    private final TeacherCourseService teacherCourseServ;

    private final AuthenticatorService authServ;

    public CourseController(CourseService courseServ, TeacherCourseService teacherCourseServ, AuthenticatorService authServ) {
        this.courseServ = courseServ;
        this.teacherCourseServ = teacherCourseServ;
        this.authServ = authServ;
    }

    @GetMapping("/course/{id}")
    public ResponseEntity<HashMap<String,Object>> getCourse(@RequestHeader("Authorization") String token, @PathVariable long id){
        if (authServ.getUserFromToken(token) == null)
            return new UnauthorizedResponse<>(null);

        Course foundCourse = courseServ.findById(id);

        if (foundCourse == null)
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);

        return new ResponseEntity<>(ProtectionService.courseWithoutPassword(foundCourse), HttpStatus.OK);
    }

    @GetMapping("/courses")
    public ResponseEntity<Iterable<HashMap<String,Object>>> getAllCourses(@RequestHeader("Authorization") String token){
        if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token))
            return new UnauthorizedResponse<>(null);

        return new ResponseEntity<>(ProtectionService.coursesWithoutPasswords(courseServ.findAll()),HttpStatus.OK);
    }

    @GetMapping("/courses/owned")
    public ResponseEntity<Iterable<HashMap<String ,Object>>> getOwnedCourses(@RequestHeader("Authorization") String token){
        if (authServ.isNotIn(new Role[]{Role.Admin,Role.Teacher},token))
            return new UnauthorizedResponse<>(null);

        return new ResponseEntity<>(ProtectionService.coursesWithoutPasswords(courseServ.findOwnedCourses(authServ.getUserFromToken(token))),HttpStatus.OK);
    }

    @GetMapping("/course/{id}/assistants")
    public ResponseEntity<Iterable<HashMap<String,Object>>> getCourseAssistants(@RequestHeader("Authorization")String token, @PathVariable long id){
        if (authServ.getUserFromToken(token) == null)
            return new UnauthorizedResponse<>(null);

        Iterable<User> assistants = teacherCourseServ.findCourseAssistants(courseServ.findById(id));

        return new ResponseEntity<>(ProtectionService.usersWithoutPasswords(assistants),HttpStatus.OK);
    }


    @PostMapping("/course")
    public ResponseEntity<Map<String ,Object>> postCourse(@RequestHeader("Authorization") String token,
                                             @RequestBody Course course)
    {
        System.out.println(course);
        System.out.println(token);
        if (authServ.isNotIn(new Role[]{Role.Secretary,Role.Admin},token))
            return new UnauthorizedResponse<>(null);

        Course createdCourse = courseServ.save(course);
        if (createdCourse == null)
            return new ResponseEntity<>(null,HttpStatus.BAD_REQUEST);

        return new ResponseEntity<>(ProtectionService.courseWithoutPassword(createdCourse), HttpStatus.CREATED);
    }


    @PatchMapping("/course/{id}")
    public ResponseEntity<Course> patchCourse(@RequestHeader("Authorization") String token,
                                              @RequestBody Map<String,Object> updates,
                                              @PathVariable long id)
    {

        if (authServ.isNotIn(new Role[]{Role.Admin,Role.Teacher,Role.Secretary}, token))
            return new UnauthorizedResponse<>(null);



        if (!courseServ.modifyData(id, updates, authServ.getUserFromToken(token).getRole()))
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);

        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/course/{id}")
    public ResponseEntity<String> postTeachers(@RequestHeader("Authorization") String token,
                                               @RequestBody Iterable<Long> teacherIds,
                                               @PathVariable Long id)
    {

        if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary}, token))
            return new UnauthorizedResponse<>(null);


        if (!teacherCourseServ.saveAll(teacherIds,courseServ.findById(id)))
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);

        return new ResponseEntity<>(HttpStatus.OK);
    }
}
