using System.Reflection;
namespace Chronos.App.Infrastructure;
///
/// Public links (BRIEF §19.4, amended 2026-10-23, R2): the Gitea repository that publishes the releases. Release
/// builds pass the repository they publish to as the ChronosReleasesUrl property.
///
public static class ProductLinks
{
/// The repository of local builds, as Directory.Build.props sets it.
public const string DefaultRepository = "https://gitea.encke-hake.ts.net/BalboSoft/Chronos";
/// "https://host/owner/name" of the releases repository.
public static string Repository { get; } = Normalise(
typeof(ProductLinks).Assembly.GetCustomAttributes()
.FirstOrDefault(a => a.Key == "ChronosReleasesUrl")?.Value) ?? DefaultRepository;
public static string Releases => $"{Repository}/releases";
/// The published stable releases, from which the update check takes the newest (BRIEF §18).
public static string StableReleasesApi => StableReleasesApiOf(Repository);
/// The licence (EULA) at the root of the releases repository (BRIEF §12.9).
public static string Licence => $"{Repository}/src/branch/main/EULA.md";
/// Where translations are reviewed by the community (BRIEF §16).
public static string Issues => $"{Repository}/issues";
///
/// The Gitea API address of a repository's published stable releases: "https://host[/prefix]/owner/name" →
/// "https://host[/prefix]/api/v1/repos/owner/name/releases?draft=false&pre-release=false&limit=50" (Gitea may
/// be served under a path prefix).
///
public static string StableReleasesApiOf(string repository)
{
var uri = new Uri(repository, UriKind.Absolute);
var segments = uri.AbsolutePath.Trim('/').Split('/', StringSplitOptions.RemoveEmptyEntries);
if (segments.Length < 2) throw new ArgumentException($"Not a repository address: {repository}", nameof(repository));
var prefix = string.Concat(segments[..^2].Select(s => "/" + s));
return $"{uri.GetLeftPart(UriPartial.Authority)}{prefix}/api/v1/repos/{segments[^2]}/{segments[^1]}/releases?draft=false&pre-release=false&limit=50";
}
/// The address without surrounding spaces, trailing slashes or ".git"; null when empty.
internal static string? Normalise(string? repository)
{
var text = repository?.Trim().TrimEnd('/');
if (text is not null && text.EndsWith(".git", StringComparison.OrdinalIgnoreCase)) text = text[..^4];
return string.IsNullOrEmpty(text) ? null : text;
}
}