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

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WK_KLI_LAPS_Service.Controllers.Master.MailTemplate
{
    [EnableCors("CorsPolicy")]
    [Authorize]
    [ApiController]
    [Route("api/[controller]")]
    public class MailTemplateApiController : Controller
    {
        CommonRepository oCommonRepository = new CommonRepository();
        private IMailTemplateService _mailTemplateService;
        public MailTemplateApiController(IMailTemplateService mailTemplateService)
        { _mailTemplateService = mailTemplateService; }

        [Authorize]
        [HttpGet]
        //Code to retrieve all data
        public IActionResult GetAll()
        {
            var _mailTemplateList = _mailTemplateService.GetAll();
            return Ok(_mailTemplateList);
        }
        [Authorize]
        [HttpGet("GetActiveTemplate")]
        //Code to retrieve all data
        public IActionResult GetActiveTemplate()
        {
            var _mailTemplateList = _mailTemplateService.GetActiveTemplate();
            return Ok(_mailTemplateList);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] mailtemplateModel omailtemplateModel)
        {

            int retval = 0;
            try
            {
                retval = _mailTemplateService.Add(omailtemplateModel);
                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] mailtemplateModel omailtemplateModel)
        {

            int retval = 0;
            try
            {
                retval = _mailTemplateService.Edit(omailtemplateModel);
                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("GetById")]
        //Code to retrieve all data
        public IActionResult GetById([FromBody] mailtemplateModel omailtemplateModel)
        {
            var _mailTemplateList = _mailTemplateService.GetById(omailtemplateModel);
            return Ok(_mailTemplateList);
        }

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


    }
}
