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

        private ICheckListDetailsService _CheckListdetailsservices;
        public CheckListDetailsApiController(ICheckListDetailsService CheckListdetailsservices)
        {
            _CheckListdetailsservices = CheckListdetailsservices;
        }

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

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

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

        [Authorize]
        [HttpGet("GetCheckList")]
        public IActionResult GetCheckList()
        {
            try
            {
                var _list = _CheckListdetailsservices.GetCheckList();
                return Ok(_list);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }

    }
}