using BPMService.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using WK_KLI_LAPS_Service.App_Data;
using WK_KLI_LAPS_Service.Models.Configuration;

namespace WK_KLI_LAPS_Service.Controllers.Configuration.Project
{
    [EnableCors("CorsPolicy")]
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class ProjectConfigurationApiController : ControllerBase
    {
        CommonRepository oCommonRepository = new CommonRepository();
        private IProjectConfigurationService _projectConfigurationService;
        public ProjectConfigurationApiController(IProjectConfigurationService projectConfigurationService)
        {
            _projectConfigurationService = projectConfigurationService;
        }
        [Authorize]
        [HttpGet]
        //Code to retrieve all data
        public IActionResult GetAll()
        {
            var _list = _projectConfigurationService.GetAll();
            return Ok(_list);
        }

        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] projectdefinitionModel oProjectdefinitionModel)
        {

            int retval = 0;
            try
            {
                retval = _projectConfigurationService.Add(oProjectdefinitionModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }

        }

        [Authorize]
        [HttpPost("GetByUserId")]
        //Code to retrieve all data
        public IActionResult GetByUserId([FromBody] dashboardModel data)
        {
            var _mappedWorkFlowDefinition = _projectConfigurationService.GetByUserId(data, "getbyuserid");
            return Ok(_mappedWorkFlowDefinition);
        }

        [Authorize]
        [HttpGet("GetActiveProjectDefinition")]
        //Code to retrieve all data
        public IActionResult GetActiveProjectDefinition()
        {
            var _list = _projectConfigurationService.GetActiveProjectDefinition();
            return Ok(_list);
        }
        [Authorize]
        [HttpPost("Update")]
        public IActionResult Update([FromBody] projectdefinitionModel oProjectdefinitionModel)
        {

            int retval = 0;
            try
            {
                retval = _projectConfigurationService.Edit(oProjectdefinitionModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }

        }
        [Authorize]
        [HttpPost("GetAllByUserId")]
        //Code to retrieve all data
        public IActionResult GetAllByUserId([FromBody] dashboardModel data)
        {
            var _mappedWorkFlowDefinition = _projectConfigurationService.GetByUserId(data, "getallbyuserid");
            return Ok(_mappedWorkFlowDefinition);
        }

        //Code to activate or deactivate specific record
        [Authorize]
        [HttpPost("activeordeactive")]
        public IActionResult Delete([FromBody] projectdefinitionModel oProjectdefinitionModel)
        {
            int retval = 0;
            try
            {
                retval = _projectConfigurationService.Delete(oProjectdefinitionModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }

        [Authorize]
        [HttpGet("GetProjectDefWorkflow")]
        //Code to retrieve all data
        public IActionResult GetProjectDefWorkflow()
        {
            var _List = _projectConfigurationService.GetProjectDefWorkflow();
            return Ok(_List);
        }

        [Authorize]
        [HttpPost("GetProjDefStageFollowup")]
        public IActionResult GetProjDefStageFollowup([FromBody] dashboardModel data)
        {
            var _List = _projectConfigurationService.GetProjDefStageFollowup(data);
            return Ok(_List);
        }


        [Authorize]
        [HttpPost("GetFlowChart")]
        //Code to retrieve all data
        public IActionResult GetFlowChart([FromBody] projectdefinitionModel oProjectdefinitionModel)
        {
            var _mappedFlowChart = _projectConfigurationService.GetFlowChart(oProjectdefinitionModel);
            return Ok(_mappedFlowChart);
        }

        //Code to get specific project definition record with master values
        [Authorize]
        [HttpPost("GetByIdWithMasterValue")]
        public IActionResult GetByIdWithMasterValue([FromBody] projectdefinitionModel oProjectdefinitionModel)
        {
            try
            {
                var list = _projectConfigurationService.GetByIdWithMasterValue(oProjectdefinitionModel);
                return Ok(list);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }

        //code to get projectworkflowdefinition by projectdefinitionid
        [Authorize]
        [HttpPost("GetByIdProjectDefWorkflow")]
        public IActionResult GetByIdProjectDefWorkflow([FromBody] projectdefinitionModel oProjectdefinitionModel)
        {
            try
            {
                var list = _projectConfigurationService.GetByIdProjectDefWorkflow(oProjectdefinitionModel);
                return Ok(list);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                return BadRequest(new { message = ex.Message });
            }
        }

        //code to get current flow by projectdefinitionid
        [Authorize]
        [HttpPost("GetCurrentFlowProject")]
        public IActionResult GetCurrentFlowProject([FromBody] projectdefinitionModel oProjectdefinitionModel)
        {
            try
            {
                var list = _projectConfigurationService.GetCurrentFlowProject(oProjectdefinitionModel);
                return Ok(list);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                return BadRequest(new { message = ex.Message });
            }
        }
        //code to get current flow by workflowid
        [Authorize]
        [HttpPost("GetCurrentFlowWorkflow")]
        public IActionResult GetCurrentFlowWorkflow([FromBody] workflowdefinitionModel data)
        {
            try
            {
                var list = _projectConfigurationService.GetCurrentFlowWorkflow(data);
                return Ok(list);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                return BadRequest(new { message = ex.Message });
            }
        }

        [Authorize]
        [HttpPost("AddStageFollowup")]
        public IActionResult AddStageFollowup([FromBody] stagefollowupModel oStagefollowupModel)
        {
            int retval = 0;
            try
            {
                retval = _projectConfigurationService.AddStageFollowup(oStagefollowupModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }

        }

        [Authorize]
        [HttpPost("EditStageFollowup")]
        public IActionResult EditStageFollowup([FromBody] stagefollowupModel oStagefollowupModel)
        {

            int retval = 0;
            try
            {
                retval = _projectConfigurationService.EditStageFollowup(oStagefollowupModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }

        }

        [Authorize]
        [HttpGet("GetStageFollowup")]
        //Code to retrieve all data
        public IActionResult GetStageFollowup()
        {
            var _mappedFlowChart = _projectConfigurationService.GetStageFollowup();
            return Ok(_mappedFlowChart);
        }


        [Authorize]
        [HttpPost("DeleteStageFollowup")]
        public IActionResult DeleteStageFollowup([FromBody] stagefollowupModel oStagefollowupModel)
        {
            int retval = 0;
            try
            {
                retval = _projectConfigurationService.DeleteStageFollowup(oStagefollowupModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }

        //code to add the project workflow definition 
        [Authorize]
        [HttpPost("AddProjectWorkflow")]
        public IActionResult AddProjectWorkflow([FromBody] List<projectworkflowdefinitionModel> oprojectworkflowdefinitionModel)
        {

            try
            {
                var list = _projectConfigurationService.AddProjectWorkflow(oprojectworkflowdefinitionModel);
                return Ok(list);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                return BadRequest(new { message = ex.Message });
            }
        }


        //code to update the project workflow definition 
        [Authorize]
        [HttpPost("UpdateProjectWorkflow")]
        public IActionResult UpdateProjectWorkflow([FromBody] List<projectworkflowdefinitionModel> oprojectworkflowdefinitionModel)
        {

            try
            {
                var list = _projectConfigurationService.UpdateProjectWorkflow(oprojectworkflowdefinitionModel);
                return Ok(list);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                return BadRequest(new { message = ex.Message });
            }
        }
        [Authorize]
        [HttpGet("GetTAT")]
        //Code to retrieve all data
        public IActionResult GetTAT()
        {
            var _list = _projectConfigurationService.GetTAT();
            return Ok(_list);
        }
        [Authorize]
        [HttpPost("getAllWOWFByUserId")]
        //Code to retrieve all data
        public IActionResult getAllWOWFByUserId([FromBody] dashboardModel data)
        {
            var _mappedWorkFlowDefinition = _projectConfigurationService.getAllWOWFByUserId(data, "getAllWOWFByUserId");
            return Ok(_mappedWorkFlowDefinition);
        }
        //code to get projectworkflowdefinition by projectdefinitionid
        [Authorize]
        [HttpPost("GetByIdProjectDefWorkflowwithOutline")]
        public IActionResult GetByIdProjectDefWorkflowwithOutline([FromBody] projectdefinitionModel oProjectdefinitionModel)
        {
            try
            {
                var list = _projectConfigurationService.GetByIdProjectDefWorkflowwithOutline(oProjectdefinitionModel);
                return Ok(list);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                return BadRequest(new { message = ex.Message });
            }
        }
        [Authorize]
        [HttpPost("GetAllForSupplement")]
        //Code to retrieve all data
        public IActionResult GetAllForSupplement([FromBody]dashboardModel data)
        {
            var _mappedWorkFlowDefinition = _projectConfigurationService.GetAllForSupplement(data, "getallforsuplement");
            return Ok(_mappedWorkFlowDefinition);
        }
        [Authorize]
        [HttpGet("GetProjectFollowup")]
        //Code to retrieve all data
        public IActionResult GetProjectFollowup()
        {
            var _mappedFlowChart = _projectConfigurationService.GetProjectFollowup();
            return Ok(_mappedFlowChart);
        }
        [Authorize]
        [HttpGet("GetActiveProjectFollowup")]
        //Code to retrieve all data
        public IActionResult GetActiveProjectFollowup()
        {
            var _GetActiveProjectFollowup = _projectConfigurationService.GetActiveProjectFollowup();
            return Ok(_GetActiveProjectFollowup);
        }
        [Authorize]
        [HttpPost("AddProjectFollowup")]
        public IActionResult AddProjectFollowup([FromBody] projectfollowupModel oStagefollowupModel)
        {
            int retval = 0;
            try
            {
                retval = _projectConfigurationService.AddProjectFollowup(oStagefollowupModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }

        }

        [Authorize]
        [HttpPost("EditProjectFollowup")]
        public IActionResult EditProjectFollowup([FromBody] projectfollowupModel oStagefollowupModel)
        {

            int retval = 0;
            try
            {
                retval = _projectConfigurationService.EditProjectFollowup(oStagefollowupModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }

        }


        [Authorize]
        [HttpPost("DeleteProjectFollowup")]
        public IActionResult DeleteProjectFollowup([FromBody] projectfollowupModel oStagefollowupModel)
        {
            int retval = 0;
            try
            {
                retval = _projectConfigurationService.DeleteProjectFollowup(oStagefollowupModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }


    }
}