using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using WK_KLI_LAPS_Service.App_Data;
using WK_KLI_LAPS_Service.Models.Master;


// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WK_KLI_LAPS_Service.Controllers.Master.StateCode
{
    [EnableCors("CorsPolicy")]
    [Authorize]
    [Route("api/[controller]")]
[ApiController]
public class StateCodeApiController : ControllerBase
{
        CommonRepository oCommonRepository = new CommonRepository();

        private IStateCodeService _StateCodeservices;
        public StateCodeApiController(IStateCodeService StateCodeServices)
        {
            _StateCodeservices = StateCodeServices;

        }

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

        [AllowAnonymous]
        [HttpPost("Edit_statecode")]
        public IActionResult Edit_statecode([FromBody] StateCodeModel data)
        {
            try
            {
                var res = _StateCodeservices.Edit_statecode(data);

                if(res.ToString() == "1")
                {
                    return BadRequest("State name is already exist");
                }
                return Ok(res);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }
        [AllowAnonymous]
        [HttpPost("Add_statecode")]
        public IActionResult Add_statecode([FromBody] StateCodeModel data)
        {
            try
            {
                var res = _StateCodeservices.Add_statecode(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 });
            }
        }
        
        [AllowAnonymous]
        [HttpPost("Active_statecode")]
        public IActionResult Active_statecode([FromBody] StateCodeModel data)
        {
            try
            {
                var res = _StateCodeservices.Active_statecode(data);
               
                return Ok();
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }

    }
}
