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

import com.itsol.quantrivanphong.exception.InputException;
import com.itsol.quantrivanphong.model.LeaveType;
import com.itsol.quantrivanphong.report.leaveform.controller.Notification;
import com.itsol.quantrivanphong.report.leavetype.business.LeaveTypeBusiness;
import com.itsol.quantrivanphong.report.leavetype.dto.LeaveTypeDTO;
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 = "/leavetype")
public class LeaveTypeController {
    @Autowired
    LeaveTypeBusiness leaveTypeBusiness;

    @PostMapping(path = "/add", consumes = "application/json", produces = "application/json")
    public ResponseEntity addLeaveForm(@RequestBody LeaveTypeDTO leaveTypeDTO) {
        String message;
        try {
            if (leaveTypeDTO.getName().trim().equals("")) throw new InputException("Tên không được để trống");
            if (leaveTypeDTO.getDescriptions().trim().equals("")) throw new InputException("Mô tả không được để trống");
            message = leaveTypeBusiness.addLeaveType(leaveTypeDTO);
        } catch (InputException e) {
            message = e.getMessage();
        }

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

    @DeleteMapping("/delete/{leaveTypeId}")
    public ResponseEntity deleteLeaveForm(@PathVariable int leaveTypeId) {
        return ResponseEntity.ok(new Notification(200, leaveTypeBusiness.deleteLeaveType(leaveTypeId)));
    }

    @PostMapping(path = "/update", consumes = "application/json", produces = "application/json")
    public ResponseEntity updateLeaveForm(@RequestBody LeaveTypeDTO leaveTypeDTO) {
        String message;

        try {
            if (leaveTypeDTO.getName().trim().equals("")) throw new InputException("Tiêu đề không được để trống");
            if (leaveTypeDTO.getDescriptions().trim().equals("")) throw new InputException("Nội dung không được để trống");
            message = leaveTypeBusiness.updateLeaveType(leaveTypeDTO);
        } catch (InputException e) {
            message = e.getMessage();
        }

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

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