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;

namespace WK_KLI_LAPS_Service.Controllers.Master.Application
{
    [EnableCors("CorsPolicy")]
    [Authorize]
    [ApiController]
    [Route("api/[controller]")]
    public class ApplicationApiController : ControllerBase
    {
        CommonRepository oCommonRepository = new CommonRepository();

        private IApplicationService _Applicationservices;
        public ApplicationApiController(IApplicationService ApplicationServices)
        {
            _Applicationservices = ApplicationServices;

        }


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

        [Authorize]
        [HttpGet("GetActiveApplication")]
        //Code to retrieve active data
        public IActionResult GetActiveApplication()
        {
            var _list = _Applicationservices.GetActiveApplication();
            return Ok(_list);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] applicationModel oapplicationModel)
        {

            int retval = 0;
            try
            {
                retval = _Applicationservices.Add(oapplicationModel);
                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 update specific record
        [Authorize]
        [HttpPost("Update")]
        public IActionResult Update([FromBody] applicationModel oapplicationModel)
        {
            int retval = 0;
            try
            {
                retval = _Applicationservices.Edit(oapplicationModel);
                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 activate or deactivate specific record
        [Authorize]
        [HttpPost("activeordeactive")]
        public IActionResult Delete([FromBody] applicationModel oapplicationModel)
        {
            int retval = 0;
            try
            {
                retval = _Applicationservices.Delete(oapplicationModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }
    }
}