using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using WK_KLI_LAPS_Service.App_Data;
using WK_KLI_LAPS_Service.Models;

namespace WK_KLI_LAPS_Service.Controllers.UserManagement.StageMapping
{
    [Route("api/[controller]")]
    [ApiController]
    public class StageMappingApiController : ControllerBase
    {
        CommonRepository oCommonRepository = new CommonRepository();
        private IStageMappingService _stagemappingservice;
        public StageMappingApiController(IStageMappingService stagemappingservice)
        {
            _stagemappingservice = stagemappingservice;
        }

        [Authorize]
        [HttpGet()]
        //Code to retrieve all data
        public IActionResult GetAll()
        {
            var _list = _stagemappingservice.GetAll();
            return Ok(_list);
        }

        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] List<userstagemappingModel> ouserprojectmappingModel)
        {
            try
            {
                string inputstr = JsonConvert.SerializeObject(ouserprojectmappingModel);
                var retval = _stagemappingservice.Add(inputstr, "insert");
                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("GetById/{id}")]
        //Code to retrieve all data
        public IActionResult GetById(int id)
        {
            var _mappedWorkFlowDefinition = _stagemappingservice.GetById(id, "getbyid");
            return Ok(_mappedWorkFlowDefinition);
        }

        [Authorize]
        [HttpPost("UpdateUserStageOnEmpty")]
        public IActionResult UpdateUserProjectOnEmpty([FromBody] userstagemappingModel ouserstagemappingModel)
        {
            try
            {
                string inputstr = JsonConvert.SerializeObject(ouserstagemappingModel);
                var _mappedouserstage = _stagemappingservice.UpdateUserProjectOnEmpty(inputstr, "updateonempty");
                return Ok(_mappedouserstage);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }

        }
        [Authorize]
        [HttpGet("GetProjectMappingUsers/{userid}")]
        //Code to retrieve all data
        public IActionResult GetProjectMappingUsers(int userid)
        {
            var _mappedWorkFlowDefinition = _stagemappingservice.GetProjectMappingUsers(userid, "getprojectmappingusers");
            return Ok(_mappedWorkFlowDefinition);
        }
        [Authorize]
        [HttpGet("GetUserStageMapping/{stagemappingid}/{userid}/{pwdid}")]
        //Code to retrieve all data
        public IActionResult GetUserStageMapping(int stagemappingid, int userid, int pwdid)
        {
            try
            {
                var _mappedWorkFlowDefinition = _stagemappingservice.GetUserStagemappingDetails(stagemappingid, userid, pwdid);
                return Ok(_mappedWorkFlowDefinition);
            }
            catch (Exception ex)
            {

                return BadRequest(ex.Message);
            }

        }
    }
}