using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure
{
    public static class FileLocator
    {
        public static string LocateXmlFile(string fileName)
        {
            string currentDirectory = Directory.GetCurrentDirectory();
            return LocateXmlFileInDirectoryAndSiblings(currentDirectory, fileName);
        }

        private static string LocateXmlFileInDirectoryAndSiblings(string directory, string fileName)
        {
            // Check the current directory
            string filePath = Path.Combine(directory, fileName);
            if (File.Exists(filePath))
            {
                return filePath;
            }

            // Check sibling directories
            string parentDirectory = Directory.GetParent(directory)?.FullName;
            if (parentDirectory != null)
            {
                foreach (var siblingDirectory in Directory.GetDirectories(parentDirectory))
                {
                    filePath = Path.Combine(siblingDirectory, fileName);
                    if (File.Exists(filePath))
                    {
                        return filePath;
                    }
                }
            }

            return null;
        }
    }
}
