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

        private IStageTypeService _StageTypeservices;
        public StageTypeApiController(IStageTypeService StageTypeServices)
        {
            _StageTypeservices = StageTypeServices;

        }


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

        [Authorize]
        [HttpGet("GetActiveStageType")]
        //Code to retrieve active data
        public IActionResult GetActiveStageType()
        {
            var _list = _StageTypeservices.GetActiveStageType();
            return Ok(_list);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] stagetypeModel ostagetypeModel)
        {

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