using System.Collections.Generic;
using System.Linq;
using AssetBank.Editor.SchemaConverter.Data.Input;

namespace AssetBank.Editor.SchemaConverter.Processors
{
    /// <summary>
    /// Handles the logic for processing PrefabInstance nodes.
    /// </summary>
    public static class PrefabProcessor
    {
        /// <summary>
        /// Creates a list of unique prefab GUIDs from all PrefabInstance nodes.
        /// </summary>
        /// <param name="prefabInstances">A list of all PrefabInstance nodes.</param>
        /// <returns>A list of unique GUID strings.</returns>
        public static (List<string> guids, List<string> warnings) Process(IReadOnlyList<OriginalNode> prefabInstances)
        {
            var warnings = new List<string>();
            var uniqueGuids = new HashSet<string>();

            foreach (var instance in prefabInstances)
            {
                var guid = instance.data?["PrefabInstance"]?["m_SourcePrefab"]?["guid"]?.ToString();
                if (!string.IsNullOrEmpty(guid))
                {
                    uniqueGuids.Add(guid);
                }
                else
                {
                    warnings.Add($"Found a PrefabInstance with anchor_id '{instance.anchor_id}' that is missing a source prefab GUID.");
                }
            }

            return (uniqueGuids.ToList(), warnings);
        }
    }
}