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

        private IToolsetService _ToolsetServices;
        public ToolsetApiController(IToolsetService ToolsetService)
        {
            _ToolsetServices = ToolsetService;
        }

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

        [Authorize]
        [HttpGet("GetActiveToolsets")]
        //Code to retrieve active data
        public IActionResult GetActiveToolsets()
        {
            var _list = _ToolsetServices.GetActiveToolsets();
            return Ok(_list);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] toolsetModel otoolsetModel)
        {

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