namespace YMhut.Box.Core.App;

public static class RuntimeLayoutPolicy
{
    public const string ReadyMarker = ".ymhut-runtime-layout";

    private static readonly string[] ExcludedRootDirectories =
    [
        "Tools",
        "Metadata",
        "data",
        "FeedbackPackages",
        "Assets",
        "config",
        "download-host",
        "plugin-host",
        "prereqs",
        "updater",
        "worker"
    ];

    public static bool ShouldSkipRelativePath(string relativePath)
    {
        var normalized = relativePath.Replace('\\', '/').Trim('/');
        var firstSegment = normalized.Split('/', 2, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
        return firstSegment is not null &&
            ExcludedRootDirectories.Any(excluded => string.Equals(firstSegment, excluded, StringComparison.OrdinalIgnoreCase));
    }

    public static bool ShouldCopyFile(string relativePath)
    {
        return !ShouldSkipRelativePath(relativePath) && !ShouldSkipFile(relativePath);
    }

    public static bool ShouldSkipFile(string relativePath)
    {
        var normalized = relativePath.Replace('\\', '/').Trim('/');
        var fileName = Path.GetFileName(relativePath);
        return fileName.Equals(ReadyMarker, StringComparison.OrdinalIgnoreCase) ||
            fileName.StartsWith("unins", StringComparison.OrdinalIgnoreCase) ||
            fileName.Equals("YMhutBox.exe", StringComparison.OrdinalIgnoreCase) ||
            fileName.Equals("YMhutBox.dll", StringComparison.OrdinalIgnoreCase) ||
            fileName.Equals("resources.pri", StringComparison.OrdinalIgnoreCase) ||
            fileName.EndsWith(".mui", StringComparison.OrdinalIgnoreCase) ||
            fileName.EndsWith(".resources.dll", StringComparison.OrdinalIgnoreCase) ||
            normalized.StartsWith("lang/", StringComparison.OrdinalIgnoreCase) ||
            normalized.StartsWith("Microsoft.UI.Xaml/", StringComparison.OrdinalIgnoreCase) ||
            normalized.StartsWith("runtimes/", StringComparison.OrdinalIgnoreCase);
    }
}
