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

        public SubSkillApiController(ISubSkillService subSkillService)
        { _subSkillService = subSkillService; }

        [Authorize]
        [HttpGet]
        //Code to retrieve all data
        public IActionResult GetAll()
        {
            var _list = _subSkillService.GetAll();
            return Ok(_list);
        }
        [Authorize]
        [HttpGet("GetActiveSubSkill")]
        //Code to retrieve all data
        public IActionResult GetActiveSubSkill()
        {
            var _list = _subSkillService.GetActiveSubSkill();
            return Ok(_list);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] subskillModel osubskillModel)
        {

            int retval = 0;
            try
            {
                retval = _subSkillService.Add(osubskillModel);
                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 add new record
        [Authorize]
        [HttpPost("Update")]
        public IActionResult Update([FromBody] subskillModel osubskillModel)
        {

            int retval = 0;
            try
            {
                retval = _subSkillService.Edit(osubskillModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }

        }

        [Authorize]
        [HttpPost("GetById")]
        //Code to retrieve all data
        public IActionResult GetById([FromBody] subskillModel osubskillModel)
        {
            var _list = _subSkillService.GetById(osubskillModel);
            return Ok(_list);
        }

        //Code to activate or deactivate specific record
        [Authorize]
        [HttpPost("activeordeactive")]
        public IActionResult Delete([FromBody] subskillModel osubskillModel)
        {
            int retval = 0;
            try
            {
                retval = _subSkillService.Delete(osubskillModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }



    }
}