package com.itsol.quantrivanphong.manager.reportdetail.controller;

import com.itsol.quantrivanphong.manager.reportdetail.bussiness.ReportProjectBussiness;
import com.itsol.quantrivanphong.manager.reportdetail.dto.request.ReportDTO;
import com.itsol.quantrivanphong.manager.reportdetail.dto.request.TimesheetRequestDTO;
import com.itsol.quantrivanphong.manager.reportdetail.dto.response.ReportResponseDTO;
import com.itsol.quantrivanphong.model.Employee;
import com.itsol.quantrivanphong.model.Project;
import com.itsol.quantrivanphong.model.ProjectReport;
import com.itsol.quantrivanphong.model.TimeSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "/admin/report")
public class ReportProjectController {
    @Autowired
    private ReportProjectBussiness reportProjectBussiness;

    //======================================================================================================================
    //Danh sách các dự án đang trong thời gian triển khai

    @GetMapping(path = "/project")
    public ResponseEntity<List<Project>> listProjectWorking(){
        List<Project> projectList = reportProjectBussiness.listProjectWorking();
        if (projectList==null){
            return (ResponseEntity<List<Project>>) ResponseEntity.status(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(projectList);
    }

    //======================================================================================================================

    //Hiển thị danh sách timesheet của các thành viên đã viết báo cáo trong ngày hôm đó

    @GetMapping(path = "/project/{projectId}/timeSheet/{currentDate}")
    public ResponseEntity<List<TimeSheet>> findAllTimeSheetStatusTrue(@PathVariable("projectId") int projectId, @PathVariable("currentDate") String currentDate){
        List<TimeSheet> timeSheetList = reportProjectBussiness.findAllTimeSheetStatusTrue(projectId, currentDate);
        if(timeSheetList == null){
            return (ResponseEntity<List<TimeSheet>>) ResponseEntity.status(HttpStatus.NOT_FOUND);
        }
        return  ResponseEntity.ok(timeSheetList);
    }


    //Nút "kiểm tra" : Lọc các thành viên chưa viết báo cáo và tự khởi tạo timesheet với trạng thái thái = false

    @PostMapping(path = "/project/{projectId}/update/{currentDate}")
    public ResponseEntity<String> updateTimeSheetStatus(@PathVariable("projectId") int projectId, @PathVariable("currentDate") String currentDate){
        String mess = reportProjectBussiness.updateTimeSheetStatus(projectId, currentDate);
        return ResponseEntity.ok(mess);
    }

    //Quản trị viên duyệt và đánh dấu (đã duyệt, từ chối) mặc định là chưa duyệt (update bảng timesheet theo id)

    @PostMapping(path = "/project/timeSheet/update/{timeSheetId}")
    public ResponseEntity<String> updateTimeSheetChecked(@RequestBody  String check, @PathVariable("timeSheetId") int timeSheetId){
        String mess = reportProjectBussiness.updateTimeSheetChecked(timeSheetId, check);
        return ResponseEntity.ok(mess);
    }


    //Danh sách thành viên trong Dự án thiếu báo cáo ngày hôm đó
    @GetMapping(path = "/project/{projectId}/allLack/{currentDate}")
    public ResponseEntity<List<Employee>> listLackOfReport(@PathVariable("projectId") int projectId, @PathVariable("currentDate") String currentDate){
        List<Employee> employeeList = reportProjectBussiness.listLackOfReport(projectId, currentDate);
        if(employeeList==null){
            return (ResponseEntity<List<Employee>>) ResponseEntity.status(HttpStatus.NOT_FOUND);
        }
        return  ResponseEntity.ok(employeeList);
    }


    //=================================================================================================================


    //Tạo một báo cáo mới
    @PostMapping(path = "/project/newReport")
    public ResponseEntity<String> insertProjectReport(@RequestBody ReportDTO reportDTO ){
        String mess = reportProjectBussiness.insertProjectReport(reportDTO);
        return ResponseEntity.ok(mess);
    }


    //Báo cáo gần nhất của dự án
    @GetMapping(path = "/project/{projectId}/latest")
    public ResponseEntity<ProjectReport> latestReport(@PathVariable("projectId") int projectId){
            ProjectReport projectReport = reportProjectBussiness.latestReport(projectId);
        return  ResponseEntity.ok(projectReport);
    }

    // Tất cả các báo cáo của dự án
    @GetMapping(path = "/project/{projectId}/allReport")
    public ResponseEntity<List<ProjectReport>> allProjectReport(@PathVariable("projectId") int projectId){
            List<ProjectReport> projectReportList = reportProjectBussiness.allProjectReport(projectId);
            if (projectReportList==null){
                return (ResponseEntity<List<ProjectReport>>) ResponseEntity.status(HttpStatus.NOT_FOUND);
            }
        return  ResponseEntity.ok(projectReportList);
    }


    //=================================================================================================================


    // Danh sách thành viên không viết timesheet

    @GetMapping(path = "/project/{projectId}/employee/{firstDate}/{finalDate}")
    public ResponseEntity<List<Employee>> listEmployeeLack(@PathVariable("projectId") int projectId, @PathVariable("firstDate") String firstDate, @PathVariable("finalDate") String finalDate){
        List<Employee> employeeList = reportProjectBussiness.listEmployeeLack(firstDate, finalDate, projectId);
        if (employeeList==null){
            return (ResponseEntity<List<Employee>>) ResponseEntity.status(HttpStatus.NOT_FOUND);
        }
        return  ResponseEntity.ok(employeeList);
    }

    // Chi tiết thiếu timesheet của mỗi thành viên
    @GetMapping(path = "/project/{projectId}/employee/{employeeId}/{firstDate}/{finalDate}")
    public ResponseEntity<ReportResponseDTO> listEmployeeLackDetail(@PathVariable("projectId") int projectId,@PathVariable("employeeId") int employeeId, @PathVariable("firstDate") String firstDate, @PathVariable("finalDate") String finalDate){
        ReportResponseDTO reportResponseDTO = new ReportResponseDTO();
        reportResponseDTO.setTimeSheetList(reportProjectBussiness.listDateOfLack(firstDate, finalDate, projectId, employeeId));
        reportResponseDTO.setNumberOflack(reportProjectBussiness.numberLack(firstDate, finalDate, projectId, employeeId));
        return  ResponseEntity.ok(reportResponseDTO);
    }




    //=================================================================================================================



}
