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.JobMeta
{
    [EnableCors("CorsPolicy")]
    [Authorize]
    [ApiController]
    [Route("api/[controller]")]
    public class JobMetaApiController : ControllerBase
    {
        CommonRepository oCommonRepository = new CommonRepository();

        private IJobMetaService _jobTypeservices;
        public JobMetaApiController(IJobMetaService JobMetaServices)
        {
            _jobTypeservices = JobMetaServices;

        }


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

        [Authorize]
        [HttpGet("GetActiveJobMeta")]
        //Code to retrieve active data
        public IActionResult GetActiveJobMeta()
        {
            var _list = _jobTypeservices.GetActiveJobMeta();
            return Ok(_list);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] jobmetaModel oallocationtypeModel)
        {

            int retval = 0;
            try
            {
                retval = _jobTypeservices.Add(oallocationtypeModel);
                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 update specific record
        [Authorize]
        [HttpPost("Update")]
        public IActionResult Update([FromBody] jobmetaModel oallocationtypeModel)
        {
            int retval = 0;
            try
            {
                retval = _jobTypeservices.Edit(oallocationtypeModel);
                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 activate or deactivate specific record
        [Authorize]
        [HttpPost("activeordeactive")]
        public IActionResult Delete([FromBody] jobmetaModel oallocationtypeModel)
        {
            int retval = 0;
            try
            {
                retval = _jobTypeservices.Delete(oallocationtypeModel);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }

        [Authorize]
        [HttpGet("Get_projectdeflst/{userid:int}")]
        public IActionResult Get_projectdeflst(int userid)
        {
            //int retval = 0;
            try
            {
                jobmetaModel lst = new jobmetaModel();
                lst.createdby = userid;
                object retval = _jobTypeservices.Get_projectdeflst(lst);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }
    }
}