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

import com.itsol.quantrivanphong.exception.InputException;
import com.itsol.quantrivanphong.model.LeaveType;
import com.itsol.quantrivanphong.report.leaveform.business.LeaveFormBusiness;
import com.itsol.quantrivanphong.report.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 java.util.List;

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

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

    @PostMapping(path = "/leaveform/add", consumes = "application/json", produces = "application/json")
    public ResponseEntity addLeaveForm(@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.addLeaveForm(leaveFormDTO);
        } catch (InputException e) {
            message = e.getMessage();
        }

        return ResponseEntity.ok(new Notification(200, message));
    }

    @DeleteMapping("/leaveform/delete/{leaveformId}")
    public ResponseEntity deleteLeaveForm(@PathVariable int leaveformId) {
        return ResponseEntity.ok(new Notification(200, leaveFormBusiness.deleteLeaveForm(leaveformId)));
    }

    @PostMapping(path = "/leaveform/update", consumes = "application/json", produces = "application/json")
    public ResponseEntity 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(new Notification(200, message));
    }

    @PostMapping(path = "/leaveform/{leaveformId}/status")
    public ResponseEntity updateLeaveFormStatus(@PathVariable int leaveformId) {
        String message;
        message = leaveFormBusiness.updateLeaveFormStatus(leaveformId);
        return ResponseEntity.ok(new Notification(200, message));
    }

    @GetMapping("/leaveform/show/{employeeId}")
    public ResponseEntity<List<LeaveForm>> showLeaveFormByEmployeeId(@PathVariable int employeeId) {
        if (leaveFormBusiness.showLeaveFormByEmployeeId(employeeId) != null) {
            return ResponseEntity.ok(leaveFormBusiness.showLeaveFormByEmployeeId(employeeId));
        } else {
            return null;
        }
    }

    @GetMapping("/leaveform/{id}")
    public ResponseEntity<LeaveForm> showLeaveFormById(@PathVariable int id) {
        return ResponseEntity.ok(leaveFormBusiness.showLeaveFormById(id));
    }

    @GetMapping(path = "/leaveType/getAll")
    public ResponseEntity<List<LeaveType>> getAllLeaveType() {
        return ResponseEntity.ok(leaveFormBusiness.getLeaveTypeList());
    }
}
