using Microsoft.AspNetCore.Authorization;
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.Menu
{
    [Route("api/[controller]")]
    [ApiController]
    public class MenuApiController : ControllerBase
    {
        CommonRepository oCommonRepository = new CommonRepository();

        private IMenuService _Menuservices;
        public MenuApiController(IMenuService MenuServices)
        {
            _Menuservices = MenuServices;

        }


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

        [Authorize]
        [HttpGet("GetActiveMenu")]
        //Code to retrieve active data
        public IActionResult GetActiveMenu()
        {
            var _list = _Menuservices.GetActiveMenu();
            return Ok(_list);
        }
        [Authorize]
        [HttpGet("GetAllWithExclude")]
        //Code to retrieve active data
        public IActionResult GetAllWithExclude()
        {
            var _list = _Menuservices.GetActiveMenu();
            return Ok(_list);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] menuModel omenuModel)
        {

            int retval = 0;
            try
            {
                retval = _Menuservices.Add(omenuModel);
                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] menuModel omenuModel)
        {
            int retval = 0;
            try
            {
                retval = _Menuservices.Edit(omenuModel);
                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] menuModel omenuModel)
        {
            int retval = 0;
            try
            {
                retval = _Menuservices.Delete(omenuModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }
    }
}