using Microsoft.AspNetCore.Mvc;
using Idoklad.Configuration;

namespace Idoklad.Controllers;

/// <summary>Service metadata endpoints. These do not require iDoklad credentials.</summary>
[ApiController]
[Produces("application/json")]
[Tags("Meta")]
public sealed class MetaController : ControllerBase
{
    private readonly IdokladSettings _settings;

    public MetaController(IdokladSettings settings) => _settings = settings;

    /// <summary>Liveness probe.</summary>
    [HttpGet("/health")]
    public IActionResult Health() => Ok(new { status = "ok" });

    /// <summary>Service name, version and language.</summary>
    [HttpGet("/version")]
    public IActionResult Version() => Ok(new
    {
        app = _settings.AppName,
        version = _settings.AppVersion,
        language = "dotnet",
        sdk = "IdokladSdk 5.3.0",
        root_path = _settings.RootPath,
    });

    /// <summary>
    /// Reports whether the service has default iDoklad credentials configured through
    /// environment variables. Per-request callers can always override them with headers.
    /// </summary>
    [HttpGet("/status")]
    public IActionResult Status() => Ok(new
    {
        default_credentials_configured = _settings.HasDefaultCredentials,
        client_id = !string.IsNullOrWhiteSpace(_settings.ClientId),
        client_secret = !string.IsNullOrWhiteSpace(_settings.ClientSecret),
        application_id = !string.IsNullOrWhiteSpace(_settings.ApplicationId),
        custom_urls = _settings.HasCustomUrls,
        idoklad_language = _settings.Language.ToString(),
    });
}
