using System.Text.Json;
using System.Text.Json.Serialization;

namespace JsonRpcEndpointSample.Rpc;

public sealed class JsonRpcResponse
{
    [JsonPropertyName("jsonrpc")]
    public string JsonRpc
    {
        get; set;
    } = "2.0";

    [JsonPropertyName("result")]
    [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
    public object? Result
    {
        get; set;
    }

    [JsonPropertyName("error")]
    [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
    public JsonRpcError? Error
    {
        get; set;
    }

    [JsonPropertyName("id")]
    public JsonElement? Id
    {
        get; set;
    }

    public static JsonRpcResponse CreateResult(JsonElement? id, object? result)
    {
        JsonRpcResponse response = new JsonRpcResponse();
        response.Id = id;
        response.Result = result;
        return response;
    }

    public static JsonRpcResponse CreateError(JsonElement? id, int code, string message)
    {
        JsonRpcResponse response = new JsonRpcResponse();
        response.Id = id;
        response.Error = new JsonRpcError();
        response.Error.Code = code;
        response.Error.Message = message;
        return response;
    }
}
