package net.shyshkin.study.fullstack.supportportal.backend.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.shyshkin.study.fullstack.supportportal.backend.domain.HttpResponse;
import net.shyshkin.study.fullstack.supportportal.backend.domain.Professor;
import net.shyshkin.study.fullstack.supportportal.backend.domain.dto.ProfessorDto;
import net.shyshkin.study.fullstack.supportportal.backend.service.ProfessorService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.UUID;

import static org.springframework.http.HttpStatus.OK;

@Slf4j
@RestController
@RequestMapping("professor")
@RequiredArgsConstructor
public class ProfessorResource {

    private final ProfessorService professorService;

    @GetMapping("home")
    public String showProfessor() {
        return "Application works";
    }

    @PostMapping("register")
    public Professor register(@RequestBody Professor professor) {
        return professorService.register(professor.getFirstName(), professor.getLastName(), professor.getEmail(), professor.getDepartment(), professor.getPosition());
    }

    @PostMapping("add")
    public ResponseEntity<Professor> addNewProfessor(@Valid  ProfessorDto professorDto) {
        log.debug("Professor DTO: {}", professorDto);
        Professor professor = professorService.addNewProfessor(professorDto);
        return ResponseEntity.ok(professor);
    }


    @PutMapping("{professorId}")
    public Professor updateProfessor(@PathVariable UUID professorId, @Valid  ProfessorDto professorDto) {
        log.debug("Professor DTO: {}", professorDto);
        return professorService.updateProfessor(professorId, professorDto);
    }

    @GetMapping("{professorId}")
    public Professor findProfessorById(@PathVariable UUID professorId) {
        return professorService.findByProfessorId(professorId);
    }

    @GetMapping("email/{email}")
    public Professor findProfessorByEmail(@PathVariable String email) {
        return professorService.findByEmail(email);
    }

    @GetMapping
    public Page<Professor> getAllProfessors(Pageable pageable) {
        return professorService.findAll(pageable);
    }

    @DeleteMapping("{professorId}")
    public HttpResponse deleteProfessor(@PathVariable UUID professorId) {
        professorService.deleteProfessor(professorId);
        return HttpResponse.builder()
                .httpStatusCode(OK.value())
                .httpStatus(OK)
                .reason(OK.getReasonPhrase())
                .message("Professor deleted successfully")
                .build();
    }

    @PutMapping("{professorId}/profile-image")
    public Professor updateProfileImage(@PathVariable UUID professorId, @RequestParam MultipartFile profileImage) {
        return professorService.updateProfileImage(professorId, profileImage);
    }

    @GetMapping(path = "{professorId}/profile-image/{filename}", produces = MediaType.IMAGE_JPEG_VALUE)
    public byte[] getProfileImageByProfessorId(@PathVariable UUID professorId, @PathVariable String filename) {
        return professorService.getImageByProfessorId(professorId, filename);
    }

    @GetMapping(path = "{professorId}/profile-image", produces = MediaType.IMAGE_JPEG_VALUE)
    public byte[] getDefaultProfileImage(@PathVariable UUID professorId) {
        return professorService.getDefaultProfileImage(professorId);
    }
}
