using System.Text.Json;

namespace YMhut.Box.Core.Tools;

public static class ToolWorkerProtocol
{
    public const string Version = "1";
    public const string Ready = "ready";
    public const string Ping = "ping";
    public const string Pong = "pong";
    public const string ExecuteTool = "executeTool";
    public const string Cancel = "cancel";
    public const string Progress = "progress";
    public const string Result = "result";
    public const string Error = "error";
    public const string Shutdown = "shutdown";

    private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web)
    {
        WriteIndented = false
    };

    public static string Serialize(ToolWorkerMessage message)
    {
        return JsonSerializer.Serialize(message, JsonOptions);
    }

    public static ToolWorkerMessage? Deserialize(string line)
    {
        return JsonSerializer.Deserialize<ToolWorkerMessage>(line, JsonOptions);
    }
}

public sealed record ToolWorkerMessage(
    string Type,
    string RequestId = "",
    string? ToolId = null,
    string? Input = null,
    bool Ok = false,
    string? Output = null,
    string? Error = null,
    ToolResultDocument? Document = null,
    string? Version = null,
    int TimeoutMs = 0,
    string? Language = null);
