using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;

namespace LLM.Editor.Analysis
{
    /// <summary>
    /// Provides information about the project's directory structure.
    /// Can list directories at the root or the contents of a specific folder.
    /// </summary>
    public class DirectoryStructureProvider : IContextProvider
    {
        public class DirectoryContents
        {
            public List<string> directories;
            public List<string> files;
        }

        public object GetContext(Object target, string qualifier)
        {
            var rootPath = Application.dataPath;
            var searchPath = rootPath;

            if (!string.IsNullOrEmpty(qualifier))
            {
                searchPath = Path.Combine(rootPath, qualifier);
            }

            if (!Directory.Exists(searchPath))
            {
                return $"Error: Directory does not exist at path: '{qualifier}'";
            }

            try
            {
                var result = new DirectoryContents
                {
                    directories = Directory.GetDirectories(searchPath).Select(Path.GetFileName).ToList(),
                    files = Directory.GetFiles(searchPath).Select(Path.GetFileName).Where(f => !f.EndsWith(".meta")).ToList()
                };
                return result;
            }
            catch (System.Exception e)
            {
                return $"Error reading directory contents for '{qualifier}': {e.Message}";
            }
        }
    }
}
