using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
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.Frequency
{
    [EnableCors("CorsPolicy")]
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
public class FrequencyApiController : ControllerBase
    {
        CommonRepository oCommonRepository = new CommonRepository();
        private IFrequencyService _Frequencyservices;
        public FrequencyApiController(IFrequencyService FrequencyServices)
        {
            _Frequencyservices = FrequencyServices;

        }

        [AllowAnonymous]
        [HttpGet("Getall")]
        public IActionResult Getall()
        {
            try
            {
                var list = _Frequencyservices.GetAll();

                return Ok(list);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }

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

        [AllowAnonymous]
        [HttpPost("Edit_frequency")]
        public IActionResult Edit_frequency(frequencyModel data)
        {
            try
            {
                var res = _Frequencyservices.Edit_frequency(data);

                return Ok();
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }

        [AllowAnonymous]
        [HttpPost("Active_frequency")]
        public IActionResult Active_frequency(frequencyModel data)
        {
            try
            {
                var res = _Frequencyservices.Active_frequency(data);

                return Ok();
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }
        
        [AllowAnonymous]
        [HttpPost("Add_frequency")]
        public IActionResult Add_frequency(frequencyModel data)
        {
            try
            {
                var res = _Frequencyservices.Add_frequency(data);
              /*  if(res.ToString() == "1")
                {
                    return BadRequest("State name is already exist");
                }*/
                return Ok();
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }


    }
}
