using System.Net;

namespace StellaOps.Feedser.Source.Common.Fetch;

/// <summary>
/// Result of fetching raw response content without persisting a document.
/// </summary>
public sealed record SourceFetchContentResult
{
    private SourceFetchContentResult(
        HttpStatusCode statusCode,
        byte[]? content,
        bool notModified,
        string? etag,
        DateTimeOffset? lastModified,
        string? contentType,
        int attempts,
        IReadOnlyDictionary<string, string>? headers)
    {
        StatusCode = statusCode;
        Content = content;
        IsNotModified = notModified;
        ETag = etag;
        LastModified = lastModified;
        ContentType = contentType;
        Attempts = attempts;
        Headers = headers;
    }

    public HttpStatusCode StatusCode { get; }

    public byte[]? Content { get; }

    public bool IsSuccess => Content is not null;

    public bool IsNotModified { get; }

    public string? ETag { get; }

    public DateTimeOffset? LastModified { get; }

    public string? ContentType { get; }

    public int Attempts { get; }

    public IReadOnlyDictionary<string, string>? Headers { get; }

    public static SourceFetchContentResult Success(
        HttpStatusCode statusCode,
        byte[] content,
        string? etag,
        DateTimeOffset? lastModified,
        string? contentType,
        int attempts,
        IReadOnlyDictionary<string, string>? headers)
        => new(statusCode, content, notModified: false, etag, lastModified, contentType, attempts, headers);

    public static SourceFetchContentResult NotModified(HttpStatusCode statusCode, int attempts)
        => new(statusCode, null, notModified: true, etag: null, lastModified: null, contentType: null, attempts, headers: null);

    public static SourceFetchContentResult Skipped(HttpStatusCode statusCode, int attempts)
        => new(statusCode, null, notModified: false, etag: null, lastModified: null, contentType: null, attempts, headers: null);
}
