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

        private IWorkFlowService _WorkFlowservices;
        public WorkFlowApiController(IWorkFlowService WorkFlowServices)
        {
            _WorkFlowservices = WorkFlowServices;

        }


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

        [Authorize]
        [HttpGet("GetActiveWorkFlow")]
        //Code to retrieve active data
        public IActionResult GetActiveWorkFlow()
        {
            var _list = _WorkFlowservices.GetActiveWorkFlow();
            return Ok(_list);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] workflowModel oworkflowModel)
        {

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