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

import com.itsol.quantrivanphong.exception.InputException;
import com.itsol.quantrivanphong.report.timesheet.business.TimeSheetBusiness;
import com.itsol.quantrivanphong.report.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 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> addTimeSheet(@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("/timesheet/delete/{timesheetId}")
    public ResponseEntity<String> deleteTimeSheet(@PathVariable int timesheetId) {
        return ResponseEntity.ok(timeSheetBusiness.deleteTimeSheet(timesheetId));
    }

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

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