using System.ComponentModel.DataAnnotations;

namespace StellaOps.Excititor.Connectors.Cisco.CSAF.Configuration;

public sealed class CiscoConnectorOptions : IValidatableObject
{
    public const string HttpClientName = "cisco-csaf";

    /// <summary>
    /// Endpoint for Cisco CSAF provider metadata discovery.
    /// </summary>
    [Required]
    public string MetadataUri { get; set; } = "https://api.security.cisco.com/.well-known/csaf/provider-metadata.json";

    /// <summary>
    /// Optional bearer token used when Cisco endpoints require authentication.
    /// </summary>
    public string? ApiToken { get; set; }

    /// <summary>
    /// How long provider metadata remains cached.
    /// </summary>
    public TimeSpan MetadataCacheDuration { get; set; } = TimeSpan.FromHours(6);

    /// <summary>
    /// Whether to prefer offline snapshots when fetching metadata.
    /// </summary>
    public bool PreferOfflineSnapshot { get; set; }

    /// <summary>
    /// When set, provider metadata will be persisted to the given file path.
    /// </summary>
    public bool PersistOfflineSnapshot { get; set; }

    public string? OfflineSnapshotPath { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (string.IsNullOrWhiteSpace(MetadataUri))
        {
            yield return new ValidationResult("MetadataUri must be provided.", new[] { nameof(MetadataUri) });
        }
        else if (!Uri.TryCreate(MetadataUri, UriKind.Absolute, out _))
        {
            yield return new ValidationResult("MetadataUri must be an absolute URI.", new[] { nameof(MetadataUri) });
        }

        if (MetadataCacheDuration <= TimeSpan.Zero)
        {
            yield return new ValidationResult("MetadataCacheDuration must be greater than zero.", new[] { nameof(MetadataCacheDuration) });
        }

        if (PersistOfflineSnapshot && string.IsNullOrWhiteSpace(OfflineSnapshotPath))
        {
            yield return new ValidationResult("OfflineSnapshotPath must be provided when PersistOfflineSnapshot is enabled.", new[] { nameof(OfflineSnapshotPath) });
        }
    }
}
