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

namespace adas_core.Controllers;

/// <summary>
/// Represents an API controller that exposes endpoints for managing service configurations at the "service-configs" route.
/// </summary>
/// <remarks>
/// Inherits from <see cref="ControllerBase"/> and is annotated with <see cref="ApiControllerAttribute"/> to follow Web API conventions.
/// </remarks>
/// <!-- aidoc:v1 sig=5c947ce -->
[Route("service-configs")]
[ApiController]
public class ServiceConfigController : ControllerBase
{
    private readonly IServiceConfigService _serviceConfigService;

    /// <summary>
    /// Initializes a new instance of the <see cref="ServiceConfigController"/>, a controller that exposes service configuration operations. Stores the supplied <paramref name="serviceConfigService"/> dependency and establishes a new trace identifier via <see cref="LogExecutionContext.TrySetTraceIdentifier"/>.
    /// </summary>
    /// <param name="serviceConfigService">The <see cref="IServiceConfigService"/> instance used by the controller to access service configuration functionality.</param>
    /// <!-- aidoc:v1 sig=80efd4f body=f82fa0e -->
    public ServiceConfigController(IServiceConfigService serviceConfigService)
    {
        LogExecutionContext.TrySetTraceIdentifier(Guid.NewGuid().ToString(), true);

        _serviceConfigService = serviceConfigService;
    }

    /// <summary>
    /// Retrieves a service configuration by its identifier. Access is restricted to users with the AuthAdmin role.
    /// </summary>
    /// <param name="id">The unique identifier of the service configuration to retrieve.</param>
    /// <returns>An <see cref="IActionResult"/> containing the requested service configuration.</returns>
    /// <!-- aidoc:v1 sig=bd2ffbf body=1db1264 -->
    [HttpGet("{id}")]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthAdmin)]
    public async Task<IActionResult> Find(string id)
    {
        var serviceConfig = await _serviceConfigService.Get(id);
        return Ok(serviceConfig);
    }
}