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

        private ICheckListService _CheckListservices;
        public CheckListApiController(ICheckListService CheckListServices)
        {
            _CheckListservices = CheckListServices;

        }


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

        [Authorize]
        [HttpGet("GetActiveCheckList")]
        //Code to retrieve active data
        public IActionResult GetActiveCheckList()
        {
            var _list = _CheckListservices.GetActiveCheckList();
            return Ok(_list);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] checklistModel ochecklistModel)
        {

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