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

import com.itsol.quantrivanphong.common.InputException;
import com.itsol.quantrivanphong.manage.timesheet.business.TimeSheetBusiness;
import com.itsol.quantrivanphong.manage.timesheet.dto.TimeSheetDTO;
import com.itsol.quantrivanphong.model.TimeSheet;
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 = "/timesheet")
public class TimeSheetController {
    @Autowired
    TimeSheetBusiness timeSheetBusiness;
    @PostMapping(path = "/insert", consumes = "application/json", produces = "application/json")
    public ResponseEntity<String> insertTimeSheet(@RequestBody TimeSheetDTO timeSheetDTO) {
        String message;

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

        return ResponseEntity.ok(message);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<String> deleteTimeSheet(@PathVariable int id) {
        return ResponseEntity.ok(timeSheetBusiness.deleteTimeSheet(id));
    }

    @GetMapping(path = "/index")
    public ResponseEntity<List<TimeSheet>> showAllTimeSheet() {
        return ResponseEntity.ok(timeSheetBusiness.findTimeSheetList());
    }

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

        try {
            if (timeSheetDTO.getTitle().trim().equals("")) throw new InputException("Tiêu đề không được để trống");
            if (timeSheetDTO.getContent().trim().equals("")) throw new InputException("Nội dung không được để trống");
            message = timeSheetBusiness.updateTimeSheet(timeSheetDTO);
        } 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 TimeSheetDTO timeSheetDTO) {
        String message;
        message = timeSheetBusiness.updateLeaveFormStatus(timeSheetDTO);
        return ResponseEntity.ok(message);
    }

    @GetMapping("/index/{id}")
    public ResponseEntity<List<TimeSheet>> showTimeSheetById(@PathVariable int id) {
        if (timeSheetBusiness.findTimeSheetById(id) != null) {
            return ResponseEntity.ok(timeSheetBusiness.findTimeSheetById(id));
        } else {
            return null;
        }
    }
}