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

        private IDepartmentService _Departmentservices;

        public DepartmentApiController(IDepartmentService DepartmentServices)
        {
            _Departmentservices = DepartmentServices;

        }

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

        [Authorize]
        [HttpGet("GetActiveDepartment")]
        //Code to retrieve active data
        public IActionResult GetActiveDepartment()
        {
            var _list = _Departmentservices.GetActiveDepartment();
            return Ok(_list);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] departmentModel odepartmentModel)
        {

            int retval = 0;
            try
            {
                retval = _Departmentservices.Add(odepartmentModel);
                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] departmentModel odepartmentModel)
        {
            int retval = 0;
            try
            {
                retval = _Departmentservices.Edit(odepartmentModel);
                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] departmentModel odepartmentModel)
        {
            int retval = 0;
            try
            {
                retval = _Departmentservices.Delete(odepartmentModel);
                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("GetByMappedUser")]
        //Code to retrieve active data
        public IActionResult GetByMappedUser()
        {
            var _list = _Departmentservices.GetByMappedUser();
            return Ok(_list);
        }

    }
}