using Logger;
using System.IO;
using Xunit;

namespace Homework.Tests
{
    public class LoggerTests
    {
        [Fact]
        public void FileLogger_WritesLogAndRotatesWhenOverMaxSize()
        {
            var tempDir = Path.Combine(Path.GetTempPath(), "homework-logger-tests");
            if (Directory.Exists(tempDir)) Directory.Delete(tempDir, true);

            var options = new FileLoggerOptions
            {
                LogDirectory = tempDir,
                FileName = "test.log",
                MaxFileSizeBytes = 1,
                MaxBackupFiles = 2,
                MinimumLevel = LogLevel.Trace
            };

            var logger = new FileLogger(options);

            logger.Info("first");
            logger.Info("second");
            logger.Info("third");

            var files = Directory.GetFiles(tempDir); 
            Assert.True(files.Length >= 1);

            // Ensure there is at least one archive file and final log
            Assert.Contains(files, f => Path.GetFileName(f).Equals("test.log") || Path.GetFileName(f).StartsWith("test_"));

            Directory.Delete(tempDir, true);
        }

        [Fact]
        public void CompositeLogger_LogsToMultipleTargetsWithoutThrowing()
        {
            var tempDir = Path.Combine(Path.GetTempPath(), "homework-logger-composite");
            if (Directory.Exists(tempDir)) Directory.Delete(tempDir, true);

            var fileOptions = new FileLoggerOptions
            {
                LogDirectory = tempDir,
                FileName = "composite.log",
                MinimumLevel = LogLevel.Trace
            };

            var consoleLogger = new ConsoleLogger(LogLevel.Trace);
            var fileLogger = new FileLogger(fileOptions);
            var composite = new CompositeLogger(consoleLogger, fileLogger);

            composite.Debug("debug message");
            composite.Error("error message");

            Assert.True(File.Exists(Path.Combine(tempDir, "composite.log")));
            Directory.Delete(tempDir, true);
        }
    }
}
