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

[CreateAssetMenu(fileName = "New Shop", menuName = "RPG/Shop")]
public class Shop : ScriptableObject
{
    [Header("Shop Info")]
    public string shopName;
    public string description;

    [Header("Shop Inventory")]
    public List<Item> availableItems = new List<Item>();

    [Header("Filters")]
    public bool sellWeapons = true;
    public bool sellArmor = true;
    public bool sellMiscellaneous = true;

    public List<Item> GetFilteredItems(ItemType? filterType = null, string searchTerm = "")
    {
        var filteredItems = availableItems.Where(item => item != null);

        // Apply type filter
        if (filterType.HasValue)
        {
            filteredItems = filteredItems.Where(item => item.itemType == filterType.Value);
        }

        // Apply search filter
        if (!string.IsNullOrEmpty(searchTerm))
        {
            filteredItems = filteredItems.Where(item => item.MatchesSearch(searchTerm));
        }

        // Apply shop type filters
        filteredItems = filteredItems.Where(item =>
        {
            switch (item.itemType)
            {
                case ItemType.Weapon:
                    return sellWeapons;
                case ItemType.Armor:
                    return sellArmor;
                case ItemType.Miscellaneous:
                case ItemType.Consumable:
                case ItemType.Tool:
                case ItemType.Accessory:
                    return sellMiscellaneous;
                default:
                    return false;
            }
        });

        return filteredItems.ToList();
    }
}
