package com.itsol.quantrivanphong.manage.leaveform.controller;

import com.itsol.quantrivanphong.exception.InputException;
import com.itsol.quantrivanphong.manage.leaveform.business.LeaveFormBusiness;
import com.itsol.quantrivanphong.manage.leaveform.dto.LeaveFormDTO;
import com.itsol.quantrivanphong.model.LeaveForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping(path = "/employee")
public class LeaveFormController {
    @Autowired
    LeaveFormBusiness leaveFormBusiness;

    @GetMapping(path = "/leaveform")
    public ResponseEntity<List<LeaveForm>> showAllLeaveForm() {
        return ResponseEntity.ok(leaveFormBusiness.findAll());
    }

    @PostMapping(path = "/{employee_Id}/leavetype/{leavetype_Id}/leaveform", consumes = "application/json", produces = "application/json")
    public ResponseEntity<String> insertLeaveForm(@PathVariable int employee_Id, @Valid @RequestBody LeaveFormDTO leaveFormDTO, @PathVariable int leavetype_Id) {
        String message;

        try {
            if (leaveFormDTO.getTitle().trim().equals("")) throw new InputException("Tiêu đề không được để trống");
            if (leaveFormDTO.getContent().trim().equals("")) throw new InputException("Nội dung không được để trống");
            message = leaveFormBusiness.insertLeaveForm(employee_Id, leaveFormDTO, leavetype_Id);
        } catch (InputException e) {
            message = e.getMessage();
        }

        return ResponseEntity.ok(message);
    }

    @DeleteMapping("/delete/{idemp}/{idleave}")
    public ResponseEntity<String> deleteLeaveForm(@PathVariable int idemp, @Valid @PathVariable int idleave) {
        return ResponseEntity.ok(leaveFormBusiness.deleteLeaveForm(idemp, idleave));
    }

    @PostMapping(path = "/update", consumes = "application/json", produces = "application/json")
    public ResponseEntity<String> updateLeaveForm(@RequestBody LeaveFormDTO leaveFormDTO) {
        String message;

        try {
            if (leaveFormDTO.getTitle().trim().equals("")) throw new InputException("Tiêu đề không được để trống");
            if (leaveFormDTO.getContent().trim().equals("")) throw new InputException("Nội dung không được để trống");
            message = leaveFormBusiness.updateLeaveForm(leaveFormDTO);
        } catch (InputException e) {
            message = e.getMessage();
        }

        return ResponseEntity.ok(message);
    }

    @PostMapping(path = "/update-status", consumes = "application/json", produces = "application/json")
    public ResponseEntity<String> updateLeaveFormStatus(@RequestBody LeaveFormDTO leaveFormDTO) {
        String message;
        message = leaveFormBusiness.updateLeaveFormStatus(leaveFormDTO);
        return ResponseEntity.ok(message);
    }

    @GetMapping("/index/{id}")
    public ResponseEntity<List<LeaveForm>> showLeaveFormById(@PathVariable int id) {
        if (leaveFormBusiness.leaveFormById(id) != null) {
            return ResponseEntity.ok(leaveFormBusiness.leaveFormById(id));
        } else {
            return null;
        }
    }
}
