using System.Text.Json.Serialization;

namespace ColaFlow.Modules.Mcp.Contracts.JsonRpc;

/// <summary>
/// JSON-RPC 2.0 error object
/// </summary>
public class JsonRpcError
{
    /// <summary>
    /// A Number that indicates the error type that occurred
    /// </summary>
    [JsonPropertyName("code")]
    public int Code { get; set; }

    /// <summary>
    /// A String providing a short description of the error
    /// </summary>
    [JsonPropertyName("message")]
    public string Message { get; set; } = string.Empty;

    /// <summary>
    /// A Primitive or Structured value that contains additional information about the error (optional)
    /// </summary>
    [JsonPropertyName("data")]
    [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
    public object? Data { get; set; }

    /// <summary>
    /// Parameterless constructor for JSON deserialization
    /// </summary>
    public JsonRpcError()
    {
    }

    /// <summary>
    /// Creates a new JSON-RPC error
    /// </summary>
    public JsonRpcError(JsonRpcErrorCode code, string message, object? data = null)
    {
        Code = (int)code;
        Message = message;
        Data = data;
    }

    /// <summary>
    /// Creates a new JSON-RPC error with custom code
    /// </summary>
    public JsonRpcError(int code, string message, object? data = null)
    {
        Code = code;
        Message = message;
        Data = data;
    }

    /// <summary>
    /// Creates a ParseError (-32700)
    /// </summary>
    public static JsonRpcError ParseError(string? details = null) =>
        new(JsonRpcErrorCode.ParseError, "Parse error", details);

    /// <summary>
    /// Creates an InvalidRequest error (-32600)
    /// </summary>
    public static JsonRpcError InvalidRequest(string? details = null) =>
        new(JsonRpcErrorCode.InvalidRequest, "Invalid Request", details);

    /// <summary>
    /// Creates a MethodNotFound error (-32601)
    /// </summary>
    public static JsonRpcError MethodNotFound(string method) =>
        new(JsonRpcErrorCode.MethodNotFound, $"Method not found: {method}");

    /// <summary>
    /// Creates an InvalidParams error (-32602)
    /// </summary>
    public static JsonRpcError InvalidParams(string? details = null) =>
        new(JsonRpcErrorCode.InvalidParams, "Invalid params", details);

    /// <summary>
    /// Creates an InternalError (-32603)
    /// </summary>
    public static JsonRpcError InternalError(string? details = null) =>
        new(JsonRpcErrorCode.InternalError, "Internal error", details);

    /// <summary>
    /// Creates an Unauthorized error (-32001)
    /// </summary>
    public static JsonRpcError Unauthorized(string? details = null) =>
        new(JsonRpcErrorCode.Unauthorized, "Unauthorized", details);

    /// <summary>
    /// Creates a Forbidden error (-32002)
    /// </summary>
    public static JsonRpcError Forbidden(string? details = null) =>
        new(JsonRpcErrorCode.Forbidden, "Forbidden", details);

    /// <summary>
    /// Creates a NotFound error (-32003)
    /// </summary>
    public static JsonRpcError NotFound(string? details = null) =>
        new(JsonRpcErrorCode.NotFound, "Not found", details);

    /// <summary>
    /// Creates a ValidationFailed error (-32004)
    /// </summary>
    public static JsonRpcError ValidationFailed(string? details = null) =>
        new(JsonRpcErrorCode.ValidationFailed, "Validation failed", details);
}
