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

namespace AaLog.Cli.Output
{
    /// Two output modes:
    ///   - Human:   single-line per record, easy to scan in a terminal.
    ///   - LlmJson: stable envelope { query, count, records } for agent consumption.
    public static class OutputWriter
    {
        private static readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings
        {
            NullValueHandling = NullValueHandling.Include,
            Formatting = Formatting.Indented,
        };

        public static void WriteHuman(IConsole console, IReadOnlyList<LogRecordDto> records)
        {
            foreach (var r in records)
            {
                console.Output.WriteLine(
                    $"[{r.TimestampLocal}] [{r.Level,-7}] {r.Component} ({r.ProcessName}#{r.ProcessId}/{r.ThreadId}) | {r.Message}");
            }
        }

        public static void WriteLlmJson(IConsole console, object query, IReadOnlyList<LogRecordDto> records)
        {
            var envelope = new
            {
                query,
                count = records.Count,
                records,
            };
            console.Output.WriteLine(JsonConvert.SerializeObject(envelope, JsonSettings));
        }
    }
}
