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.Controllers.Master.Frequency;
using WK_KLI_LAPS_Service.Models.Master;

namespace WK_KLI_LAPS_Service.Controllers.Reports
{
    [EnableCors("CorsPolicy")]
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class CategoryApiController : ControllerBase
{

        CommonRepository oCommonRepository = new CommonRepository();
        private ICategoryService _CategoryService;
        public CategoryApiController(ICategoryService CategoryService)
        {
            _CategoryService = CategoryService;

        }

        [AllowAnonymous]
        [HttpGet("Getall")]
        public IActionResult Getall()
        {
            try
            {
                var list = _CategoryService.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("GetActiveCategory")]
        //Code to retrieve active data
        public IActionResult GetActiveCategory()
        {
            var _list = _CategoryService.GetActiveCategory();
            return Ok(_list);
        }

        [AllowAnonymous]
        [HttpPost("Edit_category")]
        public IActionResult Edit_category(categoryModel data)
        {
            try
            {
                var res = _CategoryService.Edit_category(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_category")]
        public IActionResult Active_category(categoryModel data)
        {
            try
            {
                var res = _CategoryService.Active_category(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_category")]
        public IActionResult Add_category(categoryModel data)
        {
            try
            {
                var res = _CategoryService.Add_category(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 });
            }
        }


    }
}
