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

        private IServerService _Serverservices;
        public ServerApiController(IServerService ServerServices)
        {
            _Serverservices = ServerServices;

        }


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

        [Authorize]
        [HttpGet("GetActiveServer")]
        //Code to retrieve active data
        public IActionResult GetActiveServer()
        {
            var _list = _Serverservices.GetActiveServer();
            return Ok(_list);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] serverModel oserverModel)
        {

            int retval = 0;
            try
            {
                retval = _Serverservices.Add(oserverModel);
                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] serverModel oserverModel)
        {
            int retval = 0;
            try
            {
                retval = _Serverservices.Edit(oserverModel);
                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] serverModel oserverModel)
        {
            int retval = 0;
            try
            {
                retval = _Serverservices.Delete(oserverModel);
                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("GetServerType")]
        //Code to retrieve active data
        public IActionResult GetServerType()
        {
            var _list = _Serverservices.GetServertype();
            return Ok(_list);
        }
    }
}