using System;
using Serilog;
using XP.Common.Logging.Interfaces;

namespace XplorePlane.Services
{
    /// <summary>
    /// 日志服务适配器
    /// 将 Serilog ILogger 适配为 XplorePlane.Common.ILoggerService 接口
    /// </summary>
    public class LoggerServiceAdapter : ILoggerService
    {
        private readonly ILogger _logger;
        private readonly string _moduleName;

        /// <summary>
        /// 构造函数（使用全局 Logger）
        /// </summary>
        public LoggerServiceAdapter() : this(Log.Logger, null)
        {
        }

        /// <summary>
        /// 构造函数（指定 Serilog Logger）
        /// </summary>
        public LoggerServiceAdapter(ILogger logger) : this(logger, null)
        {
        }

        /// <summary>
        /// 私有构造函数（用于创建带模块名的实例）
        /// </summary>
        private LoggerServiceAdapter(ILogger logger, string moduleName)
        {
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));
            _moduleName = moduleName;
        }

        /// <summary>
        /// 为指定模块创建日志器（使用泛型自动推断类型名）
        /// </summary>
        public ILoggerService ForModule<T>()
        {
            var typeName = typeof(T).FullName ?? typeof(T).Name;
            var contextLogger = _logger.ForContext("SourceContext", typeName);
            return new LoggerServiceAdapter(contextLogger, typeName);
        }

        /// <summary>
        /// 为指定模块创建日志器（手动指定模块名）
        /// </summary>
        public ILoggerService ForModule(string moduleName)
        {
            if (string.IsNullOrWhiteSpace(moduleName))
                throw new ArgumentException("模块名不能为空", nameof(moduleName));

            var contextLogger = _logger.ForContext("SourceContext", moduleName);
            return new LoggerServiceAdapter(contextLogger, moduleName);
        }

        /// <summary>
        /// 获取当前日志器
        /// </summary>
        private ILogger GetLogger()
        {
            return _logger;
        }

        public void Debug(string message)
        {
            GetLogger().Debug(message);
        }

        public void Debug(string messageTemplate, params object[] propertyValues)
        {
            GetLogger().Debug(messageTemplate, propertyValues);
        }

        public void Info(string message)
        {
            GetLogger().Information(message);
        }

        public void Info(string messageTemplate, params object[] propertyValues)
        {
            GetLogger().Information(messageTemplate, propertyValues);
        }

        public void Warn(string message)
        {
            GetLogger().Warning(message);
        }

        public void Warn(string messageTemplate, params object[] propertyValues)
        {
            GetLogger().Warning(messageTemplate, propertyValues);
        }

        public void Error(string message)
        {
            GetLogger().Error(message);
        }

        public void Error(string messageTemplate, params object[] propertyValues)
        {
            GetLogger().Error(messageTemplate, propertyValues);
        }

        public void Error(Exception exception, string message)
        {
            GetLogger().Error(exception, message);
        }

        public void Error(Exception exception, string messageTemplate, params object[] propertyValues)
        {
            GetLogger().Error(exception, messageTemplate, propertyValues);
        }

        public void Fatal(string message)
        {
            GetLogger().Fatal(message);
        }

        public void Fatal(string messageTemplate, params object[] propertyValues)
        {
            GetLogger().Fatal(messageTemplate, propertyValues);
        }

        public void Fatal(Exception exception, string message)
        {
            GetLogger().Fatal(exception, message);
        }

        public void Fatal(Exception exception, string messageTemplate, params object[] propertyValues)
        {
            GetLogger().Fatal(exception, messageTemplate, propertyValues);
        }
    }
}
