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.Skill
{

    [EnableCors("CorsPolicy")]
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class SkillApiController : ControllerBase
    {
        CommonRepository oCommonRepository = new CommonRepository();

        private ISkillService _Skillservice;
        public SkillApiController(ISkillService SkillServices)
        {
            _Skillservice = SkillServices;
        }


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


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


        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] skillModel oskillModel)
        {
            int retval = 0;
            try
            {
                retval = _Skillservice.Add(oskillModel);
                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] skillModel oskillModel)
        {
            int retval = 0;
            try
            {
                retval = _Skillservice.Edit(oskillModel);
                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] skillModel oskillModel)
        {
            int retval = 0;
            try
            {
                retval = _Skillservice.Delete(oskillModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }
    }
}