package com.nisum.myteam.controller;


import com.nisum.myteam.exception.handler.MyTeamException;
import com.nisum.myteam.exception.handler.ResponseDetails;
import com.nisum.myteam.model.dao.Employee;
import com.nisum.myteam.model.dao.ResourceAllocation;
import com.nisum.myteam.model.vo.ResourceVO;
import com.nisum.myteam.repository.EmployeeVisaRepo;
import com.nisum.myteam.service.IEmployeeService;
import com.nisum.myteam.service.IProjectService;
import com.nisum.myteam.service.impl.ResourceAllocationService;
import com.nisum.myteam.utils.MyTeamUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping
@Slf4j
public class ResourceAllocationController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private EmployeeVisaRepo employeeVisaRepo;

    @Autowired
    private ResourceAllocationService resourceAllocService;

//tested in all the cases.ok
    @RequestMapping(value = "/resources", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> createResource(@RequestBody ResourceAllocation resourceAllocationReq,
                                            @RequestParam(value = "loginEmpId", required = true) String loginEmpId, HttpServletRequest request) throws MyTeamException {
        if (StringUtils.isNotBlank(loginEmpId)) {
            resourceAllocationReq.setAuditFields(loginEmpId, MyTeamUtils.CREATE);

            if (resourceAllocService.validateBillingStartEndDateAgainstProjectStartEndDate(resourceAllocationReq, loginEmpId)) {
                if (resourceAllocService.validateBillingStartDateAgainstDOJ(resourceAllocationReq)) {
                    if (!resourceAllocService.isResourceAssignedToAnyProject(resourceAllocationReq)) {
                        ResourceAllocation resourcePersisted = resourceAllocService.addResourceAllocation(resourceAllocationReq, loginEmpId);

                        ResponseDetails createResponseDetails = new ResponseDetails(new Date(), 601, "Resource has been created",
                                "Resource description", null, request.getContextPath(), "details", resourcePersisted);
                        return new ResponseEntity<ResponseDetails>(createResponseDetails, HttpStatus.OK);

                    }

                }
            }

            ResponseDetails responseDetails = new ResponseDetails(new Date(), 602, resourceAllocService.respMap.get("message").toString(),
                    "Error message desc", null, request.getRequestURI(), "Resource details", resourceAllocationReq);
            return new ResponseEntity<ResponseDetails>(responseDetails, HttpStatus.OK);

        }
        ResponseDetails responseDetails = new ResponseDetails(new Date(), 602, "Please provide the valid Employee Id",
                "Employee Id is not valid", null, request.getRequestURI(), "Resource details", resourceAllocationReq);
        return new ResponseEntity<ResponseDetails>(responseDetails, HttpStatus.OK);

    }

    //tested in all the cases.ok
    @RequestMapping(value = "/resources", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> updateResource(@RequestBody ResourceAllocation resourceAllocationReq,
                                            @RequestParam(value = "loginEmpId") String loginEmpId, HttpServletRequest request) throws MyTeamException {

        if (StringUtils.isNotBlank(loginEmpId)) {
            resourceAllocationReq.setAuditFields(loginEmpId, MyTeamUtils.UPDATE);

            if (resourceAllocService.isResourceExistsForProject(resourceAllocationReq.getEmployeeId(), resourceAllocationReq.getProjectId())) {
                resourceAllocService.updatePreviousResource(resourceAllocationReq, loginEmpId);
                resourceAllocService.updateResource(resourceAllocationReq, loginEmpId);
            }

            ResponseDetails createResponseDetails = new ResponseDetails(new Date(), 601, resourceAllocService.respMap.get("message").toString(),
                    "Resource description", null, request.getContextPath(), "Resource details", resourceAllocationReq);
            return new ResponseEntity<ResponseDetails>(createResponseDetails, HttpStatus.OK);
        }
        ResponseDetails responseDetails = new ResponseDetails(new Date(), 602, "Please provide the valid Employee Id",
                "Employee Id is not valid", null, request.getRequestURI(), "Resource details", resourceAllocationReq);
        return new ResponseEntity<ResponseDetails>(responseDetails, HttpStatus.OK);
    }

    //Tested Ok
    @RequestMapping(value = "/resources", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> deleteResource(@RequestBody ResourceAllocation resourceReq,
                                            @RequestParam(value = "loginEmpId", required = true) String loginEmpId, HttpServletRequest request) throws MyTeamException {
        if (StringUtils.isNotBlank(loginEmpId)) {
            resourceAllocService.deleteResource(resourceReq, loginEmpId);

            ResponseDetails createResponseDetails = new ResponseDetails(new Date(), 601, "Resource has been deleted",
                    "Resource description", null, request.getContextPath(), "Resource details", resourceReq);
            return new ResponseEntity<ResponseDetails>(createResponseDetails, HttpStatus.OK);
        }

        ResponseDetails responseDetails = new ResponseDetails(new Date(), 602, "Please provide the valid Employee Id",
                "Employee Id is not valid", null, request.getRequestURI(), "Resource details", resourceReq);
        return new ResponseEntity<ResponseDetails>(responseDetails, HttpStatus.OK);
    }




    //Ok Tested in all of the cases
    @RequestMapping(value = "/resources/project/{projectId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> getResourcesForProject(@PathVariable(value = "projectId", required = true) String projectId,
                                                    @RequestParam(value = "status", required = false, defaultValue = MyTeamUtils.ACTIVE) String status,
                                                    HttpServletRequest request)
            throws MyTeamException {

        if (StringUtils.isNotBlank(projectId)) {
            List<ResourceVO> resourcesList = resourceAllocService.getResourcesForProject(projectId, status);
            ResponseDetails responseDetails = new ResponseDetails(new Date(), 602, "Resources have been retrieved successfully",
                    "List of Resources for a project", resourcesList, request.getRequestURI(), "Resource details", null);
            return new ResponseEntity<ResponseDetails>(responseDetails, HttpStatus.OK);

        }
        ResponseDetails responseDetails = new ResponseDetails(new Date(), 602, "Please provide ProjectId",
                "List of Resources for a project", null, request.getRequestURI(), "Resource details", null);
        return new ResponseEntity<ResponseDetails>(responseDetails, HttpStatus.OK);
    }

    //ok
    ///getMyProjectAllocations
    @RequestMapping(value = "/resources/getMyProjectAllocations", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> getMyProjectAllocations(
            @RequestParam("employeeId") String employeeId, HttpServletRequest request) throws MyTeamException {

        if (employeeId != null && !"".equalsIgnoreCase(employeeId)) {
            ResponseDetails getRespDetails = new ResponseDetails(new Date(), 604, "Retrieved the project allocations successfully",
                    "Projects allocations for an employee", resourceAllocService.getWorkedProjectsForResource(employeeId), request.getRequestURI(), "Project details", null);
            return new ResponseEntity<ResponseDetails>(getRespDetails, HttpStatus.OK);
        }

        ResponseDetails getRespDetails = new ResponseDetails(new Date(), 606, "Please Provide valid employee  id",
                "Project allocations for an employee", null, request.getRequestURI(), "Project details", null);
        return new ResponseEntity<ResponseDetails>(getRespDetails, HttpStatus.OK);

    }



    //ok
    ///resourceAllocation/projects has to be changed to /resourceAllocation/activeProjects
    @RequestMapping(value = "/resources/projects", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> getResourcesAllocatedForAllProjects(HttpServletRequest request) throws MyTeamException {

        List<ResourceAllocation> resourcesList = resourceAllocService.getAllResourcesForAllActiveProjects();
        ResponseDetails responseDetails = new ResponseDetails(new Date(), 602, "Resources have been retrieved successfully",
                "List of Resources for the  projects", resourcesList, request.getRequestURI(), "Resource details", null);
        return new ResponseEntity<ResponseDetails>(responseDetails, HttpStatus.OK);

    }


    //ok //Getting Current active resource record
    @RequestMapping(value = "/resources/employeeId/{employeeId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> getResourcesSortByProjectStartDate(@PathVariable(value = "employeeId", required = true) String employeeId, HttpServletRequest request)
            throws MyTeamException {

        if (StringUtils.isNotBlank(employeeId)) {
            List<ResourceAllocation> resourcesList = resourceAllocService.getResourcesSortByBillingStartDate(employeeId);
            ResponseDetails responseDetails = new ResponseDetails(new Date(), 602, "Resources have been retrieved successfully",
                    "List of Resources for an employee", resourcesList, request.getRequestURI(), "Resource List details", null);
            return new ResponseEntity<ResponseDetails>(responseDetails, HttpStatus.OK);
        }

        ResponseDetails responseDetails = new ResponseDetails(new Date(), 602, "Please provide the valid Employee Id",
                "Employee Id is not valid", null, request.getRequestURI(), "Resource details", null);
        return new ResponseEntity<ResponseDetails>(responseDetails, HttpStatus.OK);

    }


    //ok tested
    @RequestMapping(value = "/resources/active", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> getActiveResources(@RequestParam(value = "employeeId", required = false) String employeeId, HttpServletRequest request)
            throws MyTeamException {
        if (StringUtils.isNotBlank(employeeId)) {
            List<ResourceAllocation> employeesRoles = resourceAllocService.getActiveResources(employeeId);

            ResponseDetails responseDetails = new ResponseDetails(new Date(), 602, "Resources have been retrieved successfully",
                    "List of Resources who are active in the projects", employeesRoles, request.getRequestURI(), "Resource List details", null);
            return new ResponseEntity<ResponseDetails>(responseDetails, HttpStatus.OK);

        }
        ResponseDetails responseDetails = new ResponseDetails(new Date(), 602, "Please provide the valid Employee Id",
                "Employee Id is not valid", null, request.getRequestURI(), "Resource details", null);
        return new ResponseEntity<ResponseDetails>(responseDetails, HttpStatus.OK);
    }



    //ok working
    @RequestMapping(value = "/resources/deliverylead/{deliveryLeadId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> getTeamDetails(@PathVariable(value = "deliveryLeadId", required = true) String deliveryLeadId, HttpServletRequest request)
            throws MyTeamException {

        if (StringUtils.isNotBlank(deliveryLeadId)) {
            List<ResourceAllocation> resourcesList = resourceAllocService.getResourcesUnderDeliveryLead(deliveryLeadId);

            ResponseDetails responseDetails = new ResponseDetails(new Date(), 602, "Resources have been retrieved successfully",
                    "List of Resources for a Delivery Lead", resourcesList, request.getRequestURI(), "Resource details", null);
            return new ResponseEntity<ResponseDetails>(responseDetails, HttpStatus.OK);
        }

        ResponseDetails responseDetails = new ResponseDetails(new Date(), 602, "Please provide Valid Delivery Lead Id",
                "List of Resources for DeliveryLead", null, request.getRequestURI(), "Resource details", null);
        return new ResponseEntity<ResponseDetails>(responseDetails, HttpStatus.OK);

    }

    //ok tested working
    @RequestMapping(value = "/resources/unAssignedEmployees", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> getUnAssignedEmployees(HttpServletRequest request) throws MyTeamException {
        List<Employee> employeesList = resourceAllocService.getUnAssignedEmployees();

        ResponseDetails responseDetails = new ResponseDetails(new Date(), 602, "Resources have been retrieved successfully",
                "List of Resources who are not assigned to project", employeesList, request.getRequestURI(), "Resource details", null);
        return new ResponseEntity<ResponseDetails>(responseDetails, HttpStatus.OK);
    }


     //@RequestMapping(value = "/getEmployeeBillingDetailsAll"
    @RequestMapping(value = "/resources/billing", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<ResourceAllocation>> getAllBillingsForEmployee(@RequestParam("employeeId") String employeeId)
            throws MyTeamException {
        List<ResourceAllocation> resourceAllocList = resourceAllocService.getBillingsForEmployee(employeeId);
        return new ResponseEntity<>(resourceAllocList, HttpStatus.OK);
    }

    // @RequestMapping(value = "/getEmployeeBillingDetails"
    @RequestMapping(value = "/resources/billing/project/{projectId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<ResourceAllocation>> getBillingsForProject(@PathVariable("projectId") String projectId,
                                                               @RequestParam("employeeId") String employeeId) throws MyTeamException {
        List<ResourceAllocation> resourceAllocList = resourceAllocService.getBillingsForProject(employeeId, projectId);
        return new ResponseEntity<>(resourceAllocList, HttpStatus.OK);
    }





    //    @RequestMapping(value = "/resources/shifts/{shift}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
//    public ResponseEntity<?> getResourcesForShift(@PathVariable(value = "shift", required = true) String shift, HttpServletRequest request)
//            throws MyTeamException {
//
//        if (StringUtils.isNotBlank(shift)) {
//            List<Resource> resourcesList = resourceService.getResourcesForShift(shift);
//            ResponseDetails responseDetails = new ResponseDetails(new Date(), 602, "Resources have been retrieved successfully",
//                    "List of Resources for the provided shift", resourcesList, request.getRequestURI(), "Resource List for shift", null);
//            return new ResponseEntity<ResponseDetails>(responseDetails, HttpStatus.OK);
//        }
//        ResponseDetails responseDetails = new ResponseDetails(new Date(), 602, "Please provide the valid Shift value",
//                "List of Resources for the provided shift", null, request.getRequestURI(), "Resource details", null);
//        return new ResponseEntity<ResponseDetails>(responseDetails, HttpStatus.OK);
//
//    }



//    @RequestMapping(value = "/resourceAllocation/addEmployeeToTeamWithCheck", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
//    public ResponseEntity<?> addEmployeeToTeamWithCheck(@RequestBody ResourceAllocation resourceReq,
//                                                        @RequestParam(value = "loginEmpId") String loginEmpId, HttpServletRequest request) throws MyTeamException {
//
//        if (StringUtils.isNotBlank(loginEmpId)) {
//            HashMap<String, Object> responseMap = resourceAllocService.verifyResourceAssignedToAnyProject(resourceReq, loginEmpId);
//
//            ResponseDetails responseDetails = new ResponseDetails(new Date(), 602, responseMap.get("message").toString(),
//                    "List of Resources for dashboard", responseMap.get("resourceObj"), request.getRequestURI(), "Resource details", null);
//            return new ResponseEntity<ResponseDetails>(responseDetails, HttpStatus.OK);
//        }
//        ResponseDetails responseDetails = new ResponseDetails(new Date(), 602, "Please provide Valid Employee Id",
//                "Verification of resource in Bench Project", null, request.getRequestURI(), "Resource details", resourceReq);
//        return new ResponseEntity<ResponseDetails>(responseDetails, HttpStatus.OK);
//
//    }

}
