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.Metadata
{
    [EnableCors("CorsPolicy")]
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class MetadataApiController : ControllerBase
    {

        CommonRepository oCommonRepository = new CommonRepository();
        private IMetadataService _metadataService;
        public MetadataApiController(IMetadataService metadataService)
        { _metadataService = metadataService; }

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

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

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

            int retval = 0;
            try
            {
                retval = _metadataService.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 = _metadataService.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 = _metadataService.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 = _metadataService.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 });
            }
        }
    }
}