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

import com.itsol.quantrivanphong.exception.InputException;
import com.itsol.quantrivanphong.model.Eproject;
import com.itsol.quantrivanphong.model.TimeSheet;
import com.itsol.quantrivanphong.report.leaveform.controller.Notification;
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 java.util.List;

@RestController
@RequestMapping(path = "/eproject")
public class TimeSheetController {
    @Autowired
    TimeSheetBusiness timeSheetBusiness;
    @PostMapping(path = "/timesheet/add", consumes = "application/json", produces = "application/json")
    public ResponseEntity 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.addTimeSheet(timeSheetDTO);
        } catch (InputException e) {
            message = e.getMessage();
        }

        return ResponseEntity.ok(new Notification(200, message));
    }
//    @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("/timesheet/delete/{timesheetId}")
    public ResponseEntity deleteTimeSheet(@PathVariable int timesheetId) {
        return ResponseEntity.ok(new Notification(200, 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 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(new Notification(200, 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/{employeeId}")
    public ResponseEntity<List<TimeSheet>> showTimeSheetByEmployeeId(@PathVariable int employeeId) {
        return ResponseEntity.ok(timeSheetBusiness.findTimeSheetByEmployeeId(employeeId));
    }

    @GetMapping("/timesheet/{id}")
    public ResponseEntity<TimeSheet> showTimeSheetById(@PathVariable int id) {
        return ResponseEntity.ok(timeSheetBusiness.findTimeSheetById(id));
    }


    @PostMapping(path = "/timesheet/update/check", consumes = "application/json", produces = "application/json")
    public ResponseEntity<String> updateCheck(@RequestBody TimeSheetDTO timeSheetDTO) {
        return ResponseEntity.ok(timeSheetBusiness.updateCheck(timeSheetDTO));
    }


    @GetMapping(path = "/getAll")
    public List<Eproject> getAllEproject() {
        return timeSheetBusiness.getEprojectList();
    }
}