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;

// 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.Project
{
    [EnableCors("CorsPolicy")]
    [Authorize]
    [ApiController]
    [Route("api/[controller]")]
    public class ProjectApiController : ControllerBase
    {
        CommonRepository oCommonRepository = new CommonRepository();
        private IProjectService _projectService;
        public ProjectApiController(IProjectService projectService)
        {
            _projectService = projectService;
        }

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

        [Authorize]
        [HttpGet("GetActiveProject")]
        //Code to retrieve active data
        public IActionResult GetActiveWorkFlow()
        {
            var _list = _projectService.GetActiveProject();
            return Ok(_list);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] projectModel oprojectModel)
        {

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

        //Code to add new record
        [Authorize]
        [HttpPost("GetContactByCustomerId")]
        public IActionResult GetContactByCustomerId([FromBody] projectModel oprojectModel)
        {
            try
            {
                var _list = _projectService.GetContactByCustomerId(oprojectModel);
                return Ok(_list);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }

        }
    }
}
