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.AllocationType
{
    [EnableCors("CorsPolicy")]
    [Authorize]
    [ApiController]
    [Route("api/[controller]")]
    public class AllocationTypeApiController : ControllerBase
    {
        CommonRepository oCommonRepository = new CommonRepository();

        private IAllocationTypeService _AllocationTypeservices;
        public AllocationTypeApiController(IAllocationTypeService AllocationTypeServices)
        {
            _AllocationTypeservices = AllocationTypeServices;

        }


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

        [Authorize]
        [HttpGet("GetActiveAllocationType")]
        //Code to retrieve active data
        public IActionResult GetActiveAllocationType()
        {
            var _list = _AllocationTypeservices.GetActiveAllocationType();
            return Ok(_list);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] allocationtypeModel oallocationtypeModel)
        {

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