using Asp.Versioning;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using OnlineSalesAutoCrop.CoreAPI.Models.Requests.Integrations;
using OnlineSalesAutoCrop.CoreAPI.Models.Responses;
using OnlineSalesAutoCrop.CoreAPI.Models.Responses.Integrations;
using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.Integrations;
using OnlineSalesAutoCrop.CoreAPI.Services.Services.Integrations;
using System;
using System.Threading.Tasks;

namespace OnlineSalesAutoCrop.CoreAPI.Controllers.IntegretionApi
{
    /// <summary>
	/// 
	/// </summary>
	/// <remarks>
	/// 
	/// </remarks>
	/// <param name="logger"></param>
	/// <param name="integrationService"></param>
	[Authorize]
    [ApiController]
    [ApiVersion("1.0")]
    [ValidateAntiForgeryToken]
    [Route("api/v{version:apiVersion}/Integration")]
    public class IntegrationController(ILogger<IntegrationAuthController> logger, IIntegrationService integrationService) : ControllerBase
    {

        private readonly ILogger _logger = logger;
        private readonly IIntegrationService _integrationService = integrationService;


        /// <summary>
        /// Insert or Update Customers for SAP 
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="request"></param>
        /// <returns>if created return 201 or if updated return 204 true</returns>
        [HttpPost("Customers")]
        [IgnoreAntiforgeryToken]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IntegrationLoginResponse))]
        public async Task<IActionResult> UpsertCustomers(CustomerIntegrationRequest request)
        {
            CustomerIntegrationResponse response = new CustomerIntegrationResponse();
            try
            {
                response = await _integrationService.UpsertCustomerAsync(request);
                if (!response.IsUpdated)
                {
                    response.ReturnMessage.Add($"Customer Created Successfully for CustomerNumber :{request.CustomerNumber} & CompanyCode:{request.CompanyCode}");
                    response.ReturnStatus = StatusCodes.Status201Created;
                    return StatusCode(StatusCodes.Status201Created, response);
                }
                else
                {
                    response.ReturnMessage.Add($"Customer Updated Successfully for CustomerNumber :{request.CustomerNumber} & CompanyCode:{request.CompanyCode}");
                    response.ReturnStatus = StatusCodes.Status200OK;
                    return StatusCode(StatusCodes.Status200OK, response);
                }
            }
            catch (Exception ex)
            {
                string msg = $"Exception Occur in Customer Operation {request?.CustomerName}~{request?.CompanyCode}";
                _logger.LogError(exception: ex,  msg);
                response.ReturnStatus = StatusCodes.Status500InternalServerError;
                response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message);
                return StatusCode(StatusCodes.Status500InternalServerError, response);
            }
        }

        /// <summary>
        /// Insert or Update Employees for SAP 
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="request"></param>
        /// <returns>if created return 201 or if updated return 204 true</returns>
        [HttpGet("Employees")]
        [IgnoreAntiforgeryToken]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(EmployeeIntegrationReqResponse))]
        public async Task<IActionResult> UpsertEmployee(EmployeeIntegrationRequest request)
        {
            EmployeeIntegrationReqResponse response = new EmployeeIntegrationReqResponse();
            try
            {
                bool result = await _integrationService.UpsertEmployeeAsync(request);
                if (result)
                {
                    response.ReturnMessage.Add($"Employee Created Successfully for EmployeeVendorName :{request.EmployeeVendorName} & SalesOrgCode:{request.SalesOrgCode}");
                    response.ReturnStatus = StatusCodes.Status201Created;
                    return StatusCode(StatusCodes.Status201Created, response);
                }
                else
                {
                    response.ReturnMessage.Add($"Employee Updated Successfully for EmployeeVendorName :{request.EmployeeVendorName} & SalesOrgCode:{request.SalesOrgCode}");
                    response.ReturnStatus = StatusCodes.Status204NoContent;
                    return StatusCode(StatusCodes.Status204NoContent, response);
                }
            }
            catch (Exception ex)
            {
                string msg = $"Exception Occur in Employee Operation {request?.EmployeeVendorName}~{request?.SalesOrgCode}";
                _logger.LogError(exception: ex, msg);
                response.ReturnStatus = StatusCodes.Status500InternalServerError;
                response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message);
                return StatusCode(StatusCodes.Status500InternalServerError, response);
            }
        }


    }
}