using System.IO;

namespace NexusShell.Agent;

public static class NexusAgentLogger
{
    private static readonly object Gate = new();

    public static void Info(string category, string message) => Write("INFO", category, message);

    public static void Warn(string category, string message) => Write("WARN", category, message);

    public static void Error(string category, string message, Exception? ex = null)
    {
        var detail = ex is null ? message : $"{message} — {ex.Message}";
        Write("ERROR", category, detail);
    }

    private static void Write(string level, string category, string message)
    {
        try
        {
            Directory.CreateDirectory(NexusAgentConfig.LogsDirectory);
            var fileName = $"agent-{DateTime.UtcNow:yyyyMMdd}.log";
            var path = Path.Combine(NexusAgentConfig.LogsDirectory, fileName);
            var line = $"{DateTime.UtcNow:O} [{level}] [{category}] {message}{Environment.NewLine}";
            lock (Gate)
            {
                File.AppendAllText(path, line);
            }
        }
        catch
        {
            // POC: logging must not break agent startup
        }
    }
}
