using System.Reflection;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Jiaowu.Api.Controllers;

[ApiController]
[Route("api/system")]
public sealed class SystemController : ControllerBase
{
    [AllowAnonymous]
    [HttpGet("version")]
    [ProducesResponseType<SystemVersionResponse>(StatusCodes.Status200OK)]
    public SystemVersionResponse GetVersion()
    {
        var assembly = typeof(SystemController).Assembly;
        var informationalVersion = assembly
            .GetCustomAttribute<AssemblyInformationalVersionAttribute>()?
            .InformationalVersion;
        var version = informationalVersion?.Split('+', 2)[0]
            ?? assembly.GetName().Version?.ToString(3)
            ?? "unknown";

        return new SystemVersionResponse(version);
    }
}

public sealed record SystemVersionResponse(string Version);
