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;

/// <summary>
/// Represents an API controller that handles HTTP operations related to patient appointments.
/// </summary>
/// <remarks>
/// Routes requests under the <c>appointments</c> path and exposes appointment-related endpoints via the ASP.NET Core Web API infrastructure, inheriting common controller functionality from <see cref="ControllerBase"/>.
/// </remarks>
/// <!-- aidoc:v1 sig=baedf41 -->
[Route("appointments")]
[ApiController]
public class PatientAppointmentController : ControllerBase
{
    private readonly IAppointmentService _appointmentService;


    /// <summary>
    /// Initializes a new instance of the <see cref="PatientAppointmentController"/> class, which coordinates patient appointment operations, by storing <paramref name="appointmentService"/> in a private field and establishing a trace identifier via <see cref="LogExecutionContext.TrySetTraceIdentifier"/>.
    /// </summary>
    /// <param name="appointmentService">The <see cref="IAppointmentService"/> used to handle appointment-related operations.</param>
    /// <!-- aidoc:v1 sig=582775d body=422132d -->
    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>
    /// <!-- aidoc:v1 sig=d3b0a51 body=d57751e -->
    [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();
    }
}