using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using User.Insight.Tracker.Common;
using User.Insight.Tracker.DataContracts;
using User.Insight.Tracker.Services;

namespace User.Insight.Tracker.Controllers
{
    [ApiController]
    [Route("")]
    public class AzureDataSyncController
    {
        private readonly ILogger<AzureDataSyncController> _logger;
        private readonly IAzureSyncingService _azureSyncing;

        public AzureDataSyncController(ILogger<AzureDataSyncController> logger, IAzureSyncingService syncingService, IHttpContextAccessor httpContextAccessor)
        {
            _logger = logger;
            _azureSyncing = syncingService;
        }
        [ExcludeFromCodeCoverage]
        [HttpPost]
        [Route("AzureADSync")]
        [CustomExceptionFilter]
        public ActionResult<Payload<bool>> AzureADSync()
        {
            _logger.LogInformation("UserInsightTrackerController.AzureADSync()");

            try
            {
                _azureSyncing.AzureADReadService(); // Call the SyncAzureAD method

                Payload<bool> response = new Payload<bool>
                {
                    Data = true,
                    Message = "Azure AD sync successful.",
                    IsSuccess = true
                };

                return StatusCode((int)HttpStatusCode.OK, response);
            }
            catch (Exception ex)
            {

                _logger.LogError(ex, "Error during Azure AD sync.");
                return StatusCode((int)HttpStatusCode.InternalServerError, new Payload<bool>
                {
                    Data = false,
                    Message = "Error during Azure AD sync.",
                    IsSuccess = false
                });
            }

        }

        [ExcludeFromCodeCoverage]
        [NonAction]
        public virtual ObjectResult StatusCode([ActionResultStatusCode] int statusCode, [ActionResultObjectValue] object value)
        {
            return new ObjectResult(value)
            {
                StatusCode = statusCode
            };
        }
    }
}
