using Spectre.Console;
using Spectre.Console.Rendering;

namespace HanaTui.Tui.Components;

/// <summary>
/// A timestamped log entry.
/// </summary>
public sealed class LogEntry
{
    public DateTime Time { get; init; } = DateTime.Now;
    public string Text { get; init; } = "";
    public LogLevel Level { get; init; } = LogLevel.Info;
}

public enum LogLevel { Info, Sql, Done, Warn, Error }

/// <summary>
/// Maintains a bounded list of log entries and renders them as a
/// Spectre.Console IRenderable panel, showing the most recent N lines.
/// Thread-safe.
/// </summary>
public sealed class LogPanel
{
    private const int MaxEntries = 500;
    private const int VisibleLines = 20; // shown inside the panel

    private readonly List<LogEntry> _entries = new(MaxEntries);
    private readonly object _lock = new();

    public void Add(string text)
    {
        var level = DetectLevel(text);
        var entry = new LogEntry { Time = DateTime.Now, Text = text, Level = level };

        lock (_lock)
        {
            if (_entries.Count >= MaxEntries)
                _entries.RemoveAt(0);
            _entries.Add(entry);
        }
    }

    /// <summary>
    /// Returns a renderable panel showing the last N log lines.
    /// </summary>
    public IRenderable Build()
    {
        List<LogEntry> snapshot;
        lock (_lock)
        {
            var start = Math.Max(0, _entries.Count - VisibleLines);
            snapshot = _entries.GetRange(start, _entries.Count - start);
        }

        var rows = new Grid();
        rows.AddColumn(new GridColumn().NoWrap());

        // Pad to VisibleLines so the panel doesn't resize each tick
        var padCount = VisibleLines - snapshot.Count;
        for (int i = 0; i < padCount; i++)
            rows.AddRow(new Text(""));

        foreach (var entry in snapshot)
        {
            var timeStr = entry.Time.ToString("HH:mm:ss");
            var (tag, color) = entry.Level switch
            {
                LogLevel.Sql   => ("[SQL ]", "blue"),
                LogLevel.Done  => ("[DONE]", "green"),
                LogLevel.Warn  => ("[WARN]", "yellow"),
                LogLevel.Error => ("[ERR ]", "red"),
                _              => ("[INFO]", "grey"),
            };

            // Escape any markup-like content in the raw text
            var safeText = Markup.Escape(entry.Text);

            rows.AddRow(new Markup(
                $"[dim]{timeStr}[/] [{color}]{tag}[/] {safeText}"));
        }

        return new Panel(rows)
        {
            Header = new PanelHeader("[bold] OPERATION LOG [/]"),
            Border = BoxBorder.Rounded,
            Padding = new Padding(1, 0),
        };
    }

    private static LogLevel DetectLevel(string text)
    {
        if (text.Contains("[SQL ]") || text.Contains("[SQL]"))
            return LogLevel.Sql;
        if (text.Contains("[DONE]"))
            return LogLevel.Done;
        if (text.Contains("[WARN]"))
            return LogLevel.Warn;
        if (text.Contains("[ERROR]") || text.Contains("[ERR]"))
            return LogLevel.Error;
        return LogLevel.Info;
    }
}
