using Serilog;
using Serilog.Core;
using SharepointToolbox.Infrastructure.Logging;
using System.IO;

namespace SharepointToolbox.Tests.Integration;

[Trait("Category", "Integration")]
public class LoggingIntegrationTests : IDisposable
{
    private readonly string _tempLogDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

    [Fact]
    public async Task Serilog_WritesLogFile_WhenMessageLogged()
    {
        Directory.CreateDirectory(_tempLogDir);
        var logFile = Path.Combine(_tempLogDir, "test-.log");

        var logger = new LoggerConfiguration()
            .WriteTo.File(logFile, rollingInterval: RollingInterval.Day)
            .CreateLogger();

        logger.Information("Test log message {Value}", 42);
        await logger.DisposeAsync();

        var files = Directory.GetFiles(_tempLogDir, "*.log");
        Assert.Single(files);
        var content = await File.ReadAllTextAsync(files[0]);
        Assert.Contains("Test log message 42", content);
    }

    [Fact]
    public void LogPanelSink_CanBeInstantiated_WithRichTextBox()
    {
        // Verify the sink type instantiates without throwing
        // Cannot test actual UI writes without STA thread — this is structural smoke only
        var sinkType = typeof(LogPanelSink);
        Assert.NotNull(sinkType);
        Assert.True(typeof(ILogEventSink).IsAssignableFrom(sinkType));
    }

    public void Dispose()
    {
        if (Directory.Exists(_tempLogDir))
            Directory.Delete(_tempLogDir, recursive: true);
    }
}
