using Akka.Actor;
using Akka.Cluster;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace ZB.MOM.WW.Health.Akka;

/// <summary>
/// Health check that maps the local node's Akka cluster membership status to a
/// <see cref="HealthStatus"/> through a configurable <see cref="AkkaClusterStatusPolicy"/>.
/// Register to the <see cref="ZbHealthTags.Ready"/> tag only — cluster membership is a readiness
/// concern; the <see cref="ZbHealthTags.Active"/> tier is reserved for the leader / active-node probe.
/// </summary>
/// <remarks>
/// The <see cref="ActorSystem"/> is resolved lazily from the service provider. If it is not yet
/// available — e.g. during startup before Akka is initialised — the check returns
/// <see cref="HealthStatus.Degraded"/> rather than throwing, so it is safe to register before Akka
/// is fully up.
/// </remarks>
public sealed class AkkaClusterHealthCheck : IHealthCheck
{
    private readonly IServiceProvider _serviceProvider;
    private readonly AkkaClusterStatusPolicy _policy;

    /// <summary>Initializes a new <see cref="AkkaClusterHealthCheck"/>.</summary>
    /// <param name="serviceProvider">
    /// The application service provider. The <see cref="ActorSystem"/> is resolved lazily so the
    /// check is startup-safe: if no <see cref="ActorSystem"/> is registered yet the result is Degraded.
    /// </param>
    /// <param name="policy">The status-to-health mapping policy to apply.</param>
    public AkkaClusterHealthCheck(IServiceProvider serviceProvider, AkkaClusterStatusPolicy policy)
    {
        _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
        _policy = policy ?? throw new ArgumentNullException(nameof(policy));
    }

    /// <inheritdoc />
    public Task<HealthCheckResult> CheckHealthAsync(
        HealthCheckContext context,
        CancellationToken cancellationToken = default)
    {
        var system = _serviceProvider.GetService<ActorSystem>();
        if (system is null)
            return Task.FromResult(HealthCheckResult.Degraded("ActorSystem not yet available."));

        MemberStatus status;
        try
        {
            // Cluster.Get(system).SelfMember can throw while the ActorSystem exists but the cluster
            // has not finished initialising (e.g. Akka.Cluster not yet configured →
            // ConfigurationException). The spec's startup-safety rule maps this to Degraded, not an
            // escaping exception (which the host would record as Unhealthy and pull the node from
            // rotation).
            status = Cluster.Get(system).SelfMember.Status;
        }
        catch (Exception ex) when (ex is not OperationCanceledException)
        {
            return Task.FromResult(HealthCheckResult.Degraded("Akka cluster state not yet accessible.", ex));
        }

        var health = _policy.Evaluate(status);
        var description = $"Akka cluster member status: {status}";
        var result = health switch
        {
            HealthStatus.Healthy   => HealthCheckResult.Healthy(description),
            HealthStatus.Degraded  => HealthCheckResult.Degraded(description),
            _                      => HealthCheckResult.Unhealthy(description),
        };
        return Task.FromResult(result);
    }
}
