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

import com.itsol.quantrivanphong.exception.InputException;
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<String> 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(message);
    }

    @DeleteMapping("/leaveform/delete/{leaveformId}")
    public ResponseEntity<String> deleteLeaveForm(@PathVariable int leaveformId) {
        return ResponseEntity.ok(leaveFormBusiness.deleteLeaveForm(leaveformId));
    }

    @PostMapping(path = "/leaveform/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 = "/leaveform/{leaveformId}/status")
    public ResponseEntity<String> updateLeaveFormStatus(@PathVariable int leaveformId) {
        String message;
        message = leaveFormBusiness.updateLeaveFormStatus(leaveformId);
        return ResponseEntity.ok(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));
    }
}
