using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
using System;
using WK_KLI_LAPS_Service.App_Data;
using WK_KLI_LAPS_Service.Controllers.Master.Error;
using WK_KLI_LAPS_Service.Hub;
using WK_KLI_LAPS_Service.Models.Master;

namespace WK_KLI_LAPS_Service.Controllers.Master.ErrorRemarks
{
    [EnableCors("CorsPolicy")]
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class ErrorRemarksApiController : ControllerBase
    {
        // GET: api/<ErrorRemarksApiController>

        CommonRepository oCommonRepository = new CommonRepository();
        private IErrorremarksService _errorremarksService;

        public ErrorRemarksApiController(IErrorremarksService ErrorRemarksServices)
            {
            _errorremarksService= ErrorRemarksServices;
            }

        [AllowAnonymous]
        [HttpGet("Getall")]
        public IActionResult Getall()
        {
            try
            {
                var list = _errorremarksService.GetAll();

                return Ok(list);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }

        [Authorize]
        [HttpGet("GetActiveErrorRemarks")]
        //Code to retrieve active data
        public IActionResult GetActiveError()
        {
            var _list = _errorremarksService.GetActiveErrorRemarks();
            return Ok(_list);
        }

        [AllowAnonymous]
        [HttpPost("Edit_ErrorRemarks")]
        public IActionResult Edit_Error(errorremarksModel data)
        {
            try
            {
                var res = _errorremarksService.Edit_ErrorRemarks(data);

                return Ok();
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }

        [AllowAnonymous]
        [HttpPost("Active_ErrorRemarks")]
        public IActionResult Active_Error(errorremarksModel data)
        {
            try
            {
                var res = _errorremarksService.Active_ErrorRemarks(data);

                return Ok();
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }

        [AllowAnonymous]
        [HttpPost("Add_ErrorRemarks")]
        public IActionResult Add_Error(errorremarksModel data)
        {
            try
            {
                var res = _errorremarksService.Add_ErrorRemarks(data);

                return Ok();
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }
       
        [AllowAnonymous]
        [HttpPost("GetbyErrorid")]
        public IActionResult Get_Errorlst(errorremarksModel data)
        {
            try
            {
                var res = _errorremarksService.GetbyErrorid(data);

                return Ok(res);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }

    }
}
