using System.Collections.Generic;
using CliFx.Infrastructure;
using Newtonsoft.Json;

namespace MxAccess.Cli.Output
{
    /// Single shape every command emits under --llm-json:
    ///   { "query": {...}, "ok": true|false, "results": [ {...}, ... ] }
    /// Errors keep the same shape so an agent never has to special-case the parser.
    public static class Envelope
    {
        private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            NullValueHandling = NullValueHandling.Include,
            Formatting = Formatting.Indented,
        };

        public static void Write(IConsole console, object query, bool ok, IEnumerable<object> results)
        {
            var payload = new
            {
                query,
                ok,
                results,
            };
            console.Output.WriteLine(JsonConvert.SerializeObject(payload, Settings));
        }

        public static void WriteError(IConsole console, object query, string error)
        {
            var payload = new
            {
                query,
                ok = false,
                error,
            };
            console.Output.WriteLine(JsonConvert.SerializeObject(payload, Settings));
        }
    }
}
