using OF_DL.Enumerations;
using Serilog;
using Serilog.Core;
using Serilog.Events;

namespace OF_DL.Services
{
    public class LoggingService : ILoggingService
    {
        public LoggingLevelSwitch LevelSwitch { get; }

        public LoggingService()
        {
            LevelSwitch = new LoggingLevelSwitch();
            InitializeLogger();
        }

        private void InitializeLogger()
        {
            // Set the initial level to Error (until we've read from config)
            LevelSwitch.MinimumLevel = LogEventLevel.Error;

            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.ControlledBy(LevelSwitch)
                .WriteTo.File("logs/OFDL.txt", rollingInterval: RollingInterval.Day)
                .CreateLogger();

            Log.Debug("Logging service initialized");
        }

        public void UpdateLoggingLevel(LoggingLevel newLevel)
        {
            LevelSwitch.MinimumLevel = (LogEventLevel)newLevel;
            Log.Debug("Logging level updated to: {LoggingLevel}", newLevel);
        }

        public LoggingLevel GetCurrentLoggingLevel()
        {
            return (LoggingLevel)LevelSwitch.MinimumLevel;
        }
    }
}
