namespace OCES.Resonance.Core;

/// <summary>
/// 音频文件扫描器，负责递归扫描目录并识别音频文件
/// </summary>
public class AudioFileScanner
{
    private static readonly string[] SupportedExtensions = 
    { 
        ".wav", ".mp3", ".flac", ".aiff", ".aif", ".m4a", ".ogg", ".wma", ".bwf", ".wav64" 
    };

    /// <summary>
    /// 扫描指定目录，返回所有音频文件路径
    /// </summary>
    /// <param name="directoryPath">要扫描的目录路径</param>
    /// <param name="recursive">是否递归扫描子目录</param>
    /// <returns>音频文件路径集合</returns>
    public IEnumerable<string> ScanDirectory(string directoryPath, bool recursive = true)
    {
        if (!Directory.Exists(directoryPath))
        {
            throw new DirectoryNotFoundException($"目录不存在：{directoryPath}");
        }

        var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
        
        return SupportedExtensions
            .SelectMany(ext => Directory.EnumerateFiles(directoryPath, $"*{ext}", searchOption))
            .Where(IsSupportedAudioFile)
            .Distinct(StringComparer.OrdinalIgnoreCase);
    }

    /// <summary>
    /// 判断文件是否为支持的音频格式
    /// </summary>
    /// <param name="filePath">文件路径</param>
    /// <returns>是否为支持的音频格式</returns>
    public bool IsSupportedAudioFile(string filePath)
    {
        if (string.IsNullOrWhiteSpace(filePath))
        {
            return false;
        }

        var extension = Path.GetExtension(filePath).ToLowerInvariant();
        return SupportedExtensions.Contains(extension);
    }
}
