using System.Diagnostics.CodeAnalysis;
using System.Net;

namespace Pilz.Net.Api;

public record class ApiResponse<T>(
    HttpStatusCode StatusCode,
    T? Message)
    where T : ApiMessage
{
    [MemberNotNullWhen(true, nameof(Message))]
    public bool IsOk => StatusCode == HttpStatusCode.OK && Message is not null;

    [MemberNotNull(nameof(Message))]
    public T EnsureOk()
    {
        if (!IsOk)
            throw new ApiException("Api return is not ok or message invalid: " + StatusCode, StatusCode);
        return Message;
    }
}
