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.Configuration.UserManagement.ProjectMapping
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserProjectMappingApiController : ControllerBase
    {
        CommonRepository oCommonRepository = new CommonRepository();

        private IUserProjectMappingService _UserProjectMappingService;
        public UserProjectMappingApiController(IUserProjectMappingService UserProjectMappingService)
        {
            _UserProjectMappingService = UserProjectMappingService;
        }

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

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

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