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

    [EnableCors("CorsPolicy")]
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class FeatureApiController : ControllerBase
    {

        CommonRepository oCommonRepository = new CommonRepository();
        private IFeatureService _featureService;
        public FeatureApiController(IFeatureService featureService)
        { _featureService = featureService; }

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

        [Authorize]
        [HttpGet("GetActiveFileType")]
        //Code to retrieve all data
        public IActionResult GetActiveFileType()
        {
            var _fileTypeList = _featureService.GetActivemetadata();
            return Ok(_fileTypeList);
        }

        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] featureModel ofeatureModel)
        {

            int retval = 0;
            try
            {
                retval = _featureService.Add(ofeatureModel);
                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] featureModel ofeatureModel)
        {

            int retval = 0;
            try
            {
                retval = _featureService.Edit(ofeatureModel);
                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] featureModel ofeatureModel)
        {
            var _fileTypeList = _featureService.GetById(ofeatureModel);
            return Ok(_fileTypeList);
        }

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

}