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.Mvc;

namespace adas_core.Controllers;

[Route("diagnosis")]
[ApiController]
public class DiagnosisController : ControllerBase
{
    private readonly IDiagnosisService _diagnosisService;

    public DiagnosisController(IDiagnosisService diagnosisService)
    {
        LogExecutionContext.TrySetTraceIdentifier(Guid.NewGuid().ToString(), true);

        _diagnosisService = diagnosisService;
    }

    /// <summary>
    /// Processes an ORU_R01 HL7-style request from a diagnosis payload, persisting it either synchronously or asynchronously depending on the caller's wait-for-result preference.
    /// </summary>
    /// <param name="diagnosisDto">The diagnosis payload received in the request body, containing the patient number, location, observation data, observations, diagnosis text, message timestamp, and a flag indicating whether the caller wants to wait for the result.</param>
    /// <returns>An <see cref="IActionResult"/> that returns an HTTP 200 OK response when the request is accepted for processing.</returns>
    [HttpPost]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthSome)]
    [PermissionAuthorize("Source", "DisplayId")]
    public IActionResult ProcessRequest([FromBody] DiagnosisDto diagnosisDto)
    {
        var request = new ApiRequest
        {
            TraceId = Guid.NewGuid().ToString(),
            Type = "ORU_R01",
            PatientNumber = diagnosisDto.PatientNumber,
            Location = diagnosisDto.Location,
            ObservationData = diagnosisDto.ObservationData,
            Observations = diagnosisDto.Observations,
            Diagnosis = diagnosisDto.Diagnosis,
            MessageTime = diagnosisDto.MessageTime,
            WaitResult = diagnosisDto.WaitResult
        };

        if (request.WaitResult)
            _diagnosisService.SaveRequest(request);
        else
            _diagnosisService.SaveRequestAsync(request);

        return Ok();
    }
}