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

namespace WK_KLI_LAPS_Service.Controllers.UserManagement.EmailMapping
{
    [EnableCors("CorsPolicy")]
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class UserMailMappingApiController : ControllerBase
    {
        CommonRepository oCommonRepository = new CommonRepository();
        private IUserMailMappingService _mailService;
        public UserMailMappingApiController(IUserMailMappingService mailService)
        { _mailService = mailService; }

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

        [Authorize]
        [HttpGet("FindAllOrActiveMenus")]
        //Code to retrieve all data
        public IActionResult FindAllOrActiveMenus()
        {
            var _mailList = _mailService.FindAllOrActiveMenus();
            return Ok(_mailList);
        }

        //code to retrieve list of action
        [Authorize]
        [HttpGet("GetActionList")]
        public IActionResult GetActionList()
        {
            var _actionList = _mailService.getActionLst();
            return Ok(_actionList);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] usermailmappingModel omailModel)
        {

            try
            {
                string inputstr = JsonConvert.SerializeObject(omailModel);
                var retval = _mailService.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 });
            }

        }

        //Code to add new record
        [Authorize]
        [HttpPost("Update")]
        public IActionResult Update([FromBody] usermailmappingModel omailModel)
        {
            try
            {
                string inputstr = JsonConvert.SerializeObject(omailModel);
                var _mappedWorkFlowDefinition = _mailService.Update(inputstr, "update");
                return Ok(_mappedWorkFlowDefinition);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }

        }

        [Authorize]
        [HttpGet("GetById/{mailtemplateid}")]
        //Code to retrieve all data
        public IActionResult GetById(int mailtemplateid)
        {
            var _mailList = _mailService.GetById(mailtemplateid, "getbyid");
            return Ok(_mailList);
        }

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