namespace StellaOps.Vexer.Core;

/// <summary>
/// Baseline consensus policy applying tier-based weights and enforcing justification gates.
/// </summary>
public sealed class BaselineVexConsensusPolicy : IVexConsensusPolicy
{
    private readonly VexConsensusPolicyOptions _options;

    public BaselineVexConsensusPolicy(VexConsensusPolicyOptions? options = null)
    {
        _options = options ?? new VexConsensusPolicyOptions();
    }

    public string Version => _options.Version;

    public double GetProviderWeight(VexProvider provider)
    {
        if (provider is null)
        {
            throw new ArgumentNullException(nameof(provider));
        }

        if (_options.ProviderOverrides.TryGetValue(provider.Id, out var overrideWeight))
        {
            return overrideWeight;
        }

        return provider.Kind switch
        {
            VexProviderKind.Vendor => _options.VendorWeight,
            VexProviderKind.Distro => _options.DistroWeight,
            VexProviderKind.Platform => _options.PlatformWeight,
            VexProviderKind.Hub => _options.HubWeight,
            VexProviderKind.Attestation => _options.AttestationWeight,
            _ => 0,
        };
    }

    public bool IsClaimEligible(VexClaim claim, VexProvider provider, out string? rejectionReason)
    {
        if (claim is null)
        {
            throw new ArgumentNullException(nameof(claim));
        }

        if (provider is null)
        {
            throw new ArgumentNullException(nameof(provider));
        }

        if (claim.Status is VexClaimStatus.NotAffected && claim.Justification is null)
        {
            rejectionReason = "missing_justification";
            return false;
        }

        rejectionReason = null;
        return true;
    }
}
