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.Controllers.Master.ProductionWorkFlow;
using WK_KLI_LAPS_Service.Models.Master;

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

        private IProductionWorkFlowService _productionworkflowservice;
        public ProductionWorkFlowApiController(IProductionWorkFlowService productionworkflowservice)
        {
            _productionworkflowservice = productionworkflowservice;

        }


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

        [Authorize]
        [HttpGet("GetActiveProductionWorkFlow")]
        //Code to retrieve active data
        public IActionResult GetActiveProductionWorkFlow()
        {
            var _list = _productionworkflowservice.GetActiveProductionWorkFlow();
            return Ok(_list);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] productionworkflowModel oproductionworkflowModel)
        {

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