using Microsoft.AspNetCore.Mvc;

namespace adas_core.Controllers;

[Route("Sensor")]
[ApiController]
public class SensorController : ControllerBase
{
    // private readonly IObservationService _observationService;
    // private readonly ISectionService _sectionService;
    // private readonly ILightBeaconService _lightBeaconService;
    // private readonly IPatientService _patientService;
    // private readonly ILogger<SensorController> _logger;
    // private readonly IClientMessageService _clientMessageService;
    // private readonly ISubscribersService _subscribersService;
    //
    // public SensorController(IObservationService observationService, ISectionService sectionService, IPatientService patientService, ILightBeaconService lightBeaconService, ILogger<SensorController> logger, IClientMessageService clientMessageService, ISubscribersService subscribersService)
    // {
    //     LogExecutionContext.TrySetTraceIdentifier(Guid.NewGuid().ToString(), true);
    //
    //     _observationService = observationService;
    //     _sectionService = sectionService;
    //     _patientService = patientService;
    //     _lightBeaconService = lightBeaconService;
    //     _logger = logger;
    //     _clientMessageService = clientMessageService;
    //     _subscribersService = subscribersService;
    // }
    //
    // [HttpPost]
    // [Route("")]
    // public async Task<IActionResult> ProcessRequest([FromBody] ApiRequest request)
    // {
    //     if (request != null)
    //     {
    //         try
    //         {
    //             await ProcessSensorData(request);
    //         }
    //         catch (Exception ex)
    //         {
    //             _logger.LogError("Error processing sensor request: {exMessage}", ex);
    //             return BadRequest(ex.Message);
    //         }
    //     }
    //     return Ok();
    // }
    //
    // private async Task ProcessSensorData(ApiRequest request)
    // {
    //     if (request.Observations == null)
    //     {
    //         _logger.LogError("Error processing sensor data. Request observations null. ApiRequest: {request}", request);
    //         return;
    //     }
    //     
    //     var patient = await _patientService.FindByLocation(request.Location);
    //
    //     if (patient != null)
    //     {
    //         foreach (var sensorData in request.Observations)
    //         {
    //             var sensorMappedData = await _observationService.MapObservationsByName(sensorData); 
    //             var section = await _sectionService.FindByPointOfCare(patient.UnitString);
    //             if (section == null) return;
    //
    //             var subscribers = _subscribersService.GetSubscribers().Where(s =>
    //                 (s.SubscriptionType == SubscriptionType.Box && s.Box == patient.Bed &&
    //                  s.Section == section.Id) ||
    //                 (s.SubscriptionType == SubscriptionType.Section && s.Section == section.Id)).ToList();
    //             if (sensorMappedData == null) continue;
    //             
    //             sensorMappedData.PatientId = patient.Id;
    //
    //             foreach (var subscriber in subscribers)
    //             {
    //                 await _clientMessageService.SendAsync(subscriber.Id, OperationType.Observation, sensorMappedData);
    //             }
    //         }
    //     }
    //
    //
    //     var obsWithAlert = request.Observations.FirstOrDefault(o => o.Status != ObservationStatus.Ok);
    //
    //     if (obsWithAlert == null) obsWithAlert = request.Observations.FirstOrDefault();
    //     else
    //     {
    //         obsWithAlert = request.Observations.FirstOrDefault(o => o.Status == ObservationStatus.Alert);
    //         obsWithAlert ??= request.Observations.FirstOrDefault(o => o.Status == ObservationStatus.Warning);
    //     }
    //
    //     if (obsWithAlert != null) 
    //         _lightBeaconService.GenerateColorAlert(obsWithAlert);            
    // }
}