using EonaCat.Json;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;

namespace EonaCat.Logger.EonaCatCoreLogger
{
    public sealed class JsonFileLogger : ILogger, IDisposable
    {
        // This file is part of the EonaCat project(s) which is released under the Apache License.
        // See the LICENSE file or go to https://EonaCat.com/License for full license details.

        private readonly string _categoryName;
        private readonly JsonFileLoggerOptions _options;
        private readonly string _filePath;
        private readonly LoggerScopedContext _context = new();

        private readonly BlockingCollection<string> _queue;
        private readonly Thread _workerThread;
        private readonly CancellationTokenSource _cts = new CancellationTokenSource();

        public bool IncludeCorrelationId { get; set; }
        public event EventHandler<Exception> OnException;

        public JsonFileLogger(string categoryName, JsonFileLoggerOptions options)
        {
            _categoryName = categoryName;
            _options = options;
            IncludeCorrelationId = options.IncludeCorrelationId;

            _filePath = Path.Combine(_options.LogDirectory, _options.FileName);
            _queue = new BlockingCollection<string>(10000); // bounded queue

            try
            {
                Directory.CreateDirectory(_options.LogDirectory);
            }
            catch (Exception ex)
            {
                OnException?.Invoke(this, ex);
            }

            // Start background thread
            _workerThread = new Thread(ProcessQueue) { IsBackground = true };
            _workerThread.Start();
        }

        public void SetContext(string key, string value) => _context.Set(key, value);
        public void ClearContext() => _context.Clear();
        public string GetContext(string key) => _context.Get(key);
        public IDisposable BeginScope<TState>(TState state) => _context.BeginScope(state);
        public bool IsEnabled(LogLevel logLevel) => _options.IsEnabled;

        public void Log<TState>(LogLevel logLevel, EventId eventId, TState state,
            Exception exception, Func<TState, Exception, string> formatter)
        {
            if (!IsEnabled(logLevel) || formatter == null)
            {
                return;
            }

            try
            {
                if (IncludeCorrelationId)
                {
                    var correlationId = _context.Get("CorrelationId") ?? Guid.NewGuid().ToString();
                    _context.Set("CorrelationId", correlationId);
                }

                var logObject = new Dictionary<string, object>
                {
                    { "timestamp", DateTime.UtcNow },
                    { "level", logLevel.ToString() },
                    { "category", _categoryName },
                    { "message", formatter(state, exception) },
                    { "exception", exception?.ToString() },
                    { "eventId", eventId.Id }
                };

                var contextData = _context.GetAll();
                if (contextData.Count > 0)
                {
                    logObject["context"] = contextData;
                }

                string json = JsonHelper.ToJson(logObject);

                // Enqueue, drop oldest if full
                while (!_queue.TryAdd(json))
                {
                    _queue.TryTake(out _); // drop oldest
                }
            }
            catch (Exception ex)
            {
                OnException?.Invoke(this, ex);
            }
        }

        private void ProcessQueue()
        {
            try
            {
                foreach (var log in _queue.GetConsumingEnumerable(_cts.Token))
                {
                    try
                    {
                        File.AppendAllText(_filePath, log + Environment.NewLine, Encoding.UTF8);
                    }
                    catch (Exception ex)
                    {
                        OnException?.Invoke(this, ex);
                    }
                }
            }
            catch (OperationCanceledException) { }
        }

        public void Dispose()
        {
            _cts.Cancel();
            _queue.CompleteAdding();

            try
            {
                _workerThread.Join();
            }
            catch { /* ignore */ }

            _queue.Dispose();
            _cts.Dispose();
        }
    }
}
