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("appointments")]
[ApiController]
public class PatientAppointmentController : ControllerBase
{
    private readonly IAppointmentService _appointmentService;


    public PatientAppointmentController(IAppointmentService appointmentService)
    {
        LogExecutionContext.TrySetTraceIdentifier(Guid.NewGuid().ToString(), true);

        _appointmentService = appointmentService;
    }


    /// <summary>
    /// Processes an incoming appointment request by constructing an API request of type "SIU_S12" and persisting it asynchronously.
    /// </summary>
    /// <param name="appointmentDto">The appointment data received in the request body, containing patient number, location, appointments, message time, and wait result.</param>
    /// <returns>An <see cref="IActionResult"/> that returns an HTTP 200 OK response when the request is successfully saved.</returns>
    [HttpPost]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthAdmin)]
    public IActionResult ProcessRequest([FromBody] AppointmentDto appointmentDto)
    {
        var request = new ApiRequest
        {
            TraceId = Guid.NewGuid().ToString(),
            Type = "SIU_S12",
            PatientNumber = appointmentDto.PatientNumber,
            Location = appointmentDto.Location,
            Appointments = appointmentDto.Appointments,
            MessageTime = appointmentDto.MessageTime,
            WaitResult = appointmentDto.WaitResult
        };
        _appointmentService.SaveRequestAsync(request);
        return Ok();
    }
}