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

import com.itsol.quantrivanphong.exception.InputException;
import com.itsol.quantrivanphong.model.TimeSheet;
import com.itsol.quantrivanphong.report.timesheet.business.TimeSheetBusiness;
import com.itsol.quantrivanphong.report.timesheet.dto.TimeSheetDTO;
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 = "/eproject")
public class TimeSheetController {
    @Autowired
    TimeSheetBusiness timeSheetBusiness;
//    @PostMapping(path = "/timesheet", 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("/{employee_Id}/timesheet/{timesheet_Id}")
    public ResponseEntity<String> deleteTimeSheet(@PathVariable int employee_Id, @Valid @PathVariable int timesheet_Id) {
        return ResponseEntity.ok(timeSheetBusiness.deleteTimeSheet(employee_Id, timesheet_Id));
    }

    @GetMapping(path = "/timesheet")
    public List<TimeSheet> showAllTimeSheet() {
        return timeSheetBusiness.findAll();
    }

    @PostMapping(path = "/{employee_Id}/timesheet/{timesheet_Id}", consumes = "application/json", produces = "application/json")
    public ResponseEntity<String> updateTimeSheet(@PathVariable int employee_Id, @PathVariable int timesheet_Id, @Valid @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(employee_Id, timeSheetDTO, timesheet_Id);
        } 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("/{employee_Id}/timesheet")
    public ResponseEntity<List<TimeSheet>> showTimeSheetById(@PathVariable int employee_Id) {
        return ResponseEntity.ok(timeSheetBusiness.findTimeSheetById(employee_Id));
    }
}