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 MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

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

        private ISupplementTypeService _SupplementTypeService;
        public SupplementTypeApiController(ISupplementTypeService SupplementTypeServices)
        {
            _SupplementTypeService = SupplementTypeServices;
        }

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

        [Authorize]
        [HttpGet("GetActiveSupplementType")]
        //Code to retrieve active data
        public IActionResult GetActiveSupplementType()
        {
            var _list = _SupplementTypeService.GetActiveSupplementType();
            return Ok(_list);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] supplementtypeModel osupplementtypeModel)
        {
            int retval = 0;
            try
            {
                retval = _SupplementTypeService.Add(osupplementtypeModel);
                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]
        [HttpPut("Update")]
        public IActionResult Update([FromBody] supplementtypeModel osupplementtypeModel)
        {
            int retval = 0;
            try
            {
                retval = _SupplementTypeService.Edit(osupplementtypeModel);
                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] supplementtypeModel osupplementtypeModel)
        {
            int retval = 0;
            try
            {
                retval = _SupplementTypeService.Delete(osupplementtypeModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }
    }
}
