using adas_core.Application.Services.Interfaces;
using adas_core.Authentication.Attributes;
using adas_core.Domain.Enums;
using adas_core.Domain.Models;
using adas_core.Domain.Models.DTO;
using adas_core.Logging;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace adas_core.Controllers;

[Route("alarm-observations")]
[ApiController]
public class AlarmController : ControllerBase
{
    private readonly IAlarmService _alarmService;
    private readonly ILogger<AlarmController> _logger;

    public AlarmController(ILogger<AlarmController> logger, IAlarmService alarmService)
    {
        LogExecutionContext.TrySetTraceIdentifier(Guid.NewGuid().ToString(), true);

        _logger = logger;

        _alarmService = alarmService;
    }

    [AuthorizeRoles(PermissionEnum.RolesType.AuthAdmin)]
    [HttpPost]
    [Route("")]
    public IActionResult ProcessRequest([FromBody] ObservationAlarmDto alarmDto)
    {
        var request = new ApiRequest
        {
            TraceId = Guid.NewGuid().ToString(),
            Type = "ORU_R40",
            PatientNumber = alarmDto.PatientNumber,
            Location = alarmDto.Location,
            Alarms = alarmDto.Alarms,
            MessageTime = alarmDto.MessageTime,
            WaitResult = alarmDto.WaitResult,
            ObsertationData = alarmDto.ObservationData
        };

        _logger.LogDebug("Alarm received: {request}", request.ToString());

        if (request.WaitResult) _alarmService.SaveRequest(request);
        else _alarmService.SaveRequestAsync(request);
        return Ok(request);
    }
}