using System.Text.Json;

namespace CashLineERP.Api.Infrastructure;

public sealed class ReleaseGovernanceMetadataProvider
{
    private readonly IHostEnvironment _environment;
    private readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web)
    {
        PropertyNameCaseInsensitive = true
    };

    public ReleaseGovernanceMetadataProvider(IHostEnvironment environment)
    {
        _environment = environment;
    }

    public ReleaseGovernanceMetadata GetCurrent()
    {
        foreach (var candidatePath in GetCandidatePaths())
        {
            if (!File.Exists(candidatePath))
            {
                continue;
            }

            try
            {
                var source = JsonSerializer.Deserialize<ReleaseGovernanceSource>(
                    File.ReadAllText(candidatePath),
                    _jsonOptions);

                if (source is null)
                {
                    continue;
                }

                return new ReleaseGovernanceMetadata(
                    ApiVersion: source.ApiVersion ?? "v1",
                    ReleaseVersion: source.ReleaseVersion ?? "1.0.0",
                    ReleaseChannel: source.ReleaseChannel ?? "INTERNAL",
                    SupportStatus: source.SupportStatus ?? "ACTIVE",
                    ChangelogDoc: source.ChangelogDoc ?? "api-developer-portal/changelog.md",
                    LatestReleaseNotesDoc: source.ReleaseNotesDoc ?? "api-developer-portal/release-notes/README.md",
                    ChangePolicyDoc: source.ChangePolicyDoc ?? "api-developer-portal/change-policy.md",
                    DeprecationPolicyDoc: source.DeprecationPolicyDoc ?? "api-developer-portal/deprecation-policy.md",
                    ActiveDeprecationCount: source.ActiveDeprecations?.Count ?? 0);
            }
            catch (JsonException)
            {
                continue;
            }
            catch (IOException)
            {
                continue;
            }
        }

        return new ReleaseGovernanceMetadata(
            ApiVersion: "v1",
            ReleaseVersion: "1.0.0",
            ReleaseChannel: "INTERNAL",
            SupportStatus: "ACTIVE",
            ChangelogDoc: "api-developer-portal/changelog.md",
            LatestReleaseNotesDoc: "api-developer-portal/release-notes/README.md",
            ChangePolicyDoc: "api-developer-portal/change-policy.md",
            DeprecationPolicyDoc: "api-developer-portal/deprecation-policy.md",
            ActiveDeprecationCount: 0);
    }

    private IEnumerable<string> GetCandidatePaths()
    {
        yield return Path.Combine(_environment.ContentRootPath, "release-governance.json");
        yield return Path.GetFullPath(Path.Combine(_environment.ContentRootPath, "..", "..", "..", "ops", "release", "release-governance.json"));
        yield return Path.Combine(AppContext.BaseDirectory, "release-governance.json");
    }

    private sealed record ReleaseGovernanceSource(
        string? ApiVersion,
        string? ReleaseVersion,
        string? ReleaseChannel,
        string? SupportStatus,
        string? ChangelogDoc,
        string? ReleaseNotesDoc,
        string? ChangePolicyDoc,
        string? DeprecationPolicyDoc,
        List<object>? ActiveDeprecations);
}

public sealed record ReleaseGovernanceMetadata(
    string ApiVersion,
    string ReleaseVersion,
    string ReleaseChannel,
    string SupportStatus,
    string ChangelogDoc,
    string LatestReleaseNotesDoc,
    string ChangePolicyDoc,
    string DeprecationPolicyDoc,
    int ActiveDeprecationCount);
