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.RoleMapping
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserRoleMappingApiController : ControllerBase
    {
        CommonRepository oCommonRepository = new CommonRepository();
        private IUserRoleMappingService _userRoleMappingService;
        public UserRoleMappingApiController(IUserRoleMappingService userRoleMappingService)
        {
            _userRoleMappingService = userRoleMappingService;
        }

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

        [Authorize]
        [HttpPost("UpdateRoleProjectOnEmpty")]
        public IActionResult UpdateRoleProjectOnEmpty([FromBody] userrolemappingModel oinoutfileModel)
        {
            try
            {
                string inputstr = JsonConvert.SerializeObject(oinoutfileModel);
                var _mappedWorkFlowDefinition = _userRoleMappingService.UpdateRoleProjectOnEmpty(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 });
            }
        }
    }
}