using FluentAssertions;

namespace OCES.Resonance.Core.Tests;

/// <summary>
/// AudioLibraryService 集成测试
/// </summary>
public class AudioLibraryServiceTests : IDisposable
{
    readonly AudioLibraryService _service;
    readonly string _testDirectory;

    public AudioLibraryServiceTests()
    {
        _service = new AudioLibraryService();
        _testDirectory = Path.Combine(Path.GetTempPath(), $"LibraryServiceTest_{Guid.NewGuid()}");
        Directory.CreateDirectory(_testDirectory);
    }

    public void Dispose()
    {
        if (Directory.Exists(_testDirectory))
        {
            Directory.Delete(_testDirectory, true);
        }
        // 清理测试数据库
        var dbPath = "default.db";
        if (File.Exists(dbPath))
        {
            File.Delete(dbPath);
        }
    }

    [Fact]
    public async Task ScanAndImportToLibrary_空目录_返回零总数()
    {
        // Act
        var result = await _service.ScanAndImportToLibrary(_testDirectory);

        // Assert
        result.TotalFiles.Should().Be(0);
        result.SuccessCount.Should().Be(0);
        result.ErrorCount.Should().Be(0);
    }

    [Fact]
    public async Task ScanAndImportToLibrary_包含音频文件_成功导入()
    {
        // Arrange
        var wavFile = Path.Combine(_testDirectory, "test.wav");
        File.WriteAllText(wavFile, "fake wav content");

        // Act
        var result = await _service.ScanAndImportToLibrary(_testDirectory);

        // Assert
        result.TotalFiles.Should().Be(1);
        result.SuccessCount.Should().Be(1);
        result.ErrorCount.Should().Be(0);
    }

    [Fact]
    public async Task ScanAndImportToLibrary_多个文件_正确统计()
    {
        // Arrange
        File.WriteAllText(Path.Combine(_testDirectory, "audio1.wav"), "fake");
        File.WriteAllText(Path.Combine(_testDirectory, "audio2.mp3"), "fake");
        File.WriteAllText(Path.Combine(_testDirectory, "audio3.flac"), "fake");
        File.WriteAllText(Path.Combine(_testDirectory, "document.txt"), "not audio");

        // Act
        var result = await _service.ScanAndImportToLibrary(_testDirectory);

        // Assert
        result.TotalFiles.Should().Be(3);
        result.SuccessCount.Should().Be(3);
    }

    [Fact]
    public async Task ScanAndImportToLibrary_递归扫描子目录()
    {
        // Arrange
        var subDir = Path.Combine(_testDirectory, "subdir");
        Directory.CreateDirectory(subDir);
        File.WriteAllText(Path.Combine(_testDirectory, "root.wav"), "fake");
        File.WriteAllText(Path.Combine(subDir, "sub.wav"), "fake");

        // Act
        var result = await _service.ScanAndImportToLibrary(_testDirectory);

        // Assert
        result.TotalFiles.Should().Be(2);
        result.SuccessCount.Should().Be(2);
    }

    [Fact]
    public void ScanResult_GetSummary_返回正确格式()
    {
        // Arrange
        var result = new ScanResult
        {
            TotalFiles = 10,
            SuccessCount = 8,
            SkipCount = 1,
            ErrorCount = 1,
        };

        // Act
        var summary = result.GetSummary();

        // Assert
        summary.Should().Contain("10");
        summary.Should().Contain("8");
        summary.Should().Contain("1");
    }

    [Fact]
    public async Task ScanAndImportToLibrary_包含不支持格式_只导入音频()
    {
        // Arrange
        File.WriteAllText(Path.Combine(_testDirectory, "audio.wav"), "fake");
        File.WriteAllText(Path.Combine(_testDirectory, "image.jpg"), "fake");
        File.WriteAllText(Path.Combine(_testDirectory, "doc.pdf"), "fake");
        File.WriteAllText(Path.Combine(_testDirectory, "text.txt"), "fake");

        // Act
        var result = await _service.ScanAndImportToLibrary(_testDirectory);

        // Assert
        result.TotalFiles.Should().Be(1);
        result.SuccessCount.Should().Be(1);
    }
}
