using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;

public class ItemCreatorHelper
{
    [MenuItem("RPG Tools/Create Sample Items")]
    public static void CreateSampleItems()
    {
        CreateSampleWeapons();
        CreateSampleArmor();
        CreateSampleConsumables();
        CreateSampleMiscItems();

        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();

        Debug.Log("Sample items created successfully!");
    }

    private static void CreateSampleWeapons()
    {
        EnsureFolderExists("Assets/Resources/Items/Weapons");

        // Simple Sword
        var simpleSword = ScriptableObject.CreateInstance<WeaponItem>();
        simpleSword.itemName = "Simple Sword";
        simpleSword.description = "A basic iron blade, reliable and sturdy";
        simpleSword.goldCost = 15;
        simpleSword.silverCost = 0;
        simpleSword.copperCost = 0;
        simpleSword.itemType = ItemType.Weapon;
        simpleSword.rarity = ItemRarity.Common;
        simpleSword.weaponType = WeaponType.Sword;
        simpleSword.minDamage = 4;
        simpleSword.maxDamage = 8;
        simpleSword.searchTags = new string[] { "melee", "blade", "sword", "iron" };
        AssetDatabase.CreateAsset(simpleSword, "Assets/Resources/Items/Weapons/SimpleSword.asset");

        // War Axe
        var warAxe = ScriptableObject.CreateInstance<WeaponItem>();
        warAxe.itemName = "War Axe";
        warAxe.description = "A heavy two-handed axe that cleaves through armor";
        warAxe.goldCost = 22;
        warAxe.itemType = ItemType.Weapon;
        warAxe.rarity = ItemRarity.Common;
        warAxe.weaponType = WeaponType.Axe;
        warAxe.minDamage = 6;
        warAxe.maxDamage = 12;
        warAxe.searchTags = new string[] { "melee", "axe", "two-handed", "heavy" };
        AssetDatabase.CreateAsset(warAxe, "Assets/Resources/Items/Weapons/WarAxe.asset");

        // Hunting Bow
        var huntingBow = ScriptableObject.CreateInstance<WeaponItem>();
        huntingBow.itemName = "Hunting Bow";
        huntingBow.description = "A reliable ranged weapon favored by hunters";
        huntingBow.goldCost = 18;
        huntingBow.itemType = ItemType.Weapon;
        huntingBow.rarity = ItemRarity.Common;
        huntingBow.weaponType = WeaponType.Bow;
        huntingBow.minDamage = 3;
        huntingBow.maxDamage = 6;
        huntingBow.range = 30;
        huntingBow.searchTags = new string[] { "ranged", "bow", "hunting", "arrows" };
        AssetDatabase.CreateAsset(huntingBow, "Assets/Resources/Items/Weapons/HuntingBow.asset");

        // Iron Dagger
        var ironDagger = ScriptableObject.CreateInstance<WeaponItem>();
        ironDagger.itemName = "Iron Dagger";
        ironDagger.description = "Quick and light, perfect for swift strikes";
        ironDagger.goldCost = 8;
        ironDagger.itemType = ItemType.Weapon;
        ironDagger.rarity = ItemRarity.Common;
        ironDagger.weaponType = WeaponType.Dagger;
        ironDagger.minDamage = 2;
        ironDagger.maxDamage = 5;
        ironDagger.searchTags = new string[] { "melee", "dagger", "quick", "light", "iron" };
        AssetDatabase.CreateAsset(ironDagger, "Assets/Resources/Items/Weapons/IronDagger.asset");
    }

    private static void CreateSampleArmor()
    {
        EnsureFolderExists("Assets/Resources/Items/Armor");

        // Leather Vest
        var leatherVest = ScriptableObject.CreateInstance<ArmorItem>();
        leatherVest.itemName = "Leather Vest";
        leatherVest.description = "Light chest protection made from quality leather";
        leatherVest.goldCost = 10;
        leatherVest.itemType = ItemType.Armor;
        leatherVest.rarity = ItemRarity.Common;
        leatherVest.armorType = ArmorType.Light;
        leatherVest.armorSlot = ArmorSlot.Chest;
        leatherVest.armorClass = 2;
        leatherVest.searchTags = new string[] { "light", "leather", "chest", "vest" };
        AssetDatabase.CreateAsset(leatherVest, "Assets/Resources/Items/Armor/LeatherVest.asset");

        // Iron Helmet
        var ironHelmet = ScriptableObject.CreateInstance<ArmorItem>();
        ironHelmet.itemName = "Iron Helmet";
        ironHelmet.description = "Sturdy head protection forged from iron";
        ironHelmet.goldCost = 15;
        ironHelmet.itemType = ItemType.Armor;
        ironHelmet.rarity = ItemRarity.Common;
        ironHelmet.armorType = ArmorType.Medium;
        ironHelmet.armorSlot = ArmorSlot.Head;
        ironHelmet.armorClass = 3;
        ironHelmet.searchTags = new string[] { "medium", "iron", "head", "helmet" };
        AssetDatabase.CreateAsset(ironHelmet, "Assets/Resources/Items/Armor/IronHelmet.asset");

        // Chainmail Shirt
        var chainmail = ScriptableObject.CreateInstance<ArmorItem>();
        chainmail.itemName = "Chainmail Shirt";
        chainmail.description = "Interlocked metal rings provide excellent protection";
        chainmail.goldCost = 35;
        chainmail.itemType = ItemType.Armor;
        chainmail.rarity = ItemRarity.Uncommon;
        chainmail.armorType = ArmorType.Medium;
        chainmail.armorSlot = ArmorSlot.Chest;
        chainmail.armorClass = 5;
        chainmail.searchTags = new string[] { "medium", "chainmail", "chest", "metal" };
        AssetDatabase.CreateAsset(chainmail, "Assets/Resources/Items/Armor/ChainmailShirt.asset");
    }

    private static void CreateSampleConsumables()
    {
        EnsureFolderExists("Assets/Resources/Items/Miscellaneous");

        // Health Potion
        var healthPotion = ScriptableObject.CreateInstance<MiscellaneousItem>();
        healthPotion.itemName = "Health Potion";
        healthPotion.description = "A red liquid that restores 25 health points";
        healthPotion.goldCost = 5;
        healthPotion.itemType = ItemType.Consumable;
        healthPotion.rarity = ItemRarity.Common;
        healthPotion.isConsumable = true;
        healthPotion.searchTags = new string[] { "potion", "healing", "health", "red" };
        AssetDatabase.CreateAsset(healthPotion, "Assets/Resources/Items/Miscellaneous/HealthPotion.asset");

        // Mana Potion
        var manaPotion = ScriptableObject.CreateInstance<MiscellaneousItem>();
        manaPotion.itemName = "Mana Potion";
        manaPotion.description = "A blue elixir that restores 15 mana points";
        manaPotion.goldCost = 7;
        manaPotion.itemType = ItemType.Consumable;
        manaPotion.rarity = ItemRarity.Common;
        manaPotion.isConsumable = true;
        manaPotion.searchTags = new string[] { "potion", "mana", "magic", "blue" };
        AssetDatabase.CreateAsset(manaPotion, "Assets/Resources/Items/Miscellaneous/ManaPotion.asset");

        // Antidote
        var antidote = ScriptableObject.CreateInstance<MiscellaneousItem>();
        antidote.itemName = "Antidote";
        antidote.description = "Neutralizes most common poisons";
        antidote.goldCost = 3;
        antidote.itemType = ItemType.Consumable;
        antidote.rarity = ItemRarity.Common;
        antidote.isConsumable = true;
        antidote.searchTags = new string[] { "antidote", "poison", "cure", "healing" };
        AssetDatabase.CreateAsset(antidote, "Assets/Resources/Items/Miscellaneous/Antidote.asset");
    }

    private static void CreateSampleMiscItems()
    {
        EnsureFolderExists("Assets/Resources/Items/Miscellaneous");

        // Hemp Rope
        var rope = ScriptableObject.CreateInstance<MiscellaneousItem>();
        rope.itemName = "Hemp Rope";
        rope.description = "50 feet of sturdy rope, useful for climbing";
        rope.goldCost = 2;
        rope.itemType = ItemType.Tool;
        rope.rarity = ItemRarity.Common;
        rope.isConsumable = false;
        rope.searchTags = new string[] { "rope", "climbing", "tool", "hemp" };
        AssetDatabase.CreateAsset(rope, "Assets/Resources/Items/Miscellaneous/HempRope.asset");

        // Torch
        var torch = ScriptableObject.CreateInstance<MiscellaneousItem>();
        torch.itemName = "Torch";
        torch.description = "Provides light in dark places";
        torch.goldCost = 0;
        torch.silverCost = 5;
        torch.itemType = ItemType.Tool;
        torch.rarity = ItemRarity.Common;
        torch.isConsumable = true;
        torch.searchTags = new string[] { "torch", "light", "fire", "tool" };
        AssetDatabase.CreateAsset(torch, "Assets/Resources/Items/Miscellaneous/Torch.asset");

        // Lockpicks
        var lockpicks = ScriptableObject.CreateInstance<MiscellaneousItem>();
        lockpicks.itemName = "Lockpicks";
        lockpicks.description = "Fine tools for opening locks";
        lockpicks.goldCost = 8;
        lockpicks.itemType = ItemType.Tool;
        lockpicks.rarity = ItemRarity.Common;
        lockpicks.isConsumable = false;
        lockpicks.searchTags = new string[] { "lockpicks", "tools", "thief", "locks" };
        AssetDatabase.CreateAsset(lockpicks, "Assets/Resources/Items/Miscellaneous/Lockpicks.asset");

        // Camping Kit
        var campingKit = ScriptableObject.CreateInstance<MiscellaneousItem>();
        campingKit.itemName = "Camping Kit";
        campingKit.description = "Everything needed for a comfortable night outdoors";
        campingKit.goldCost = 15;
        campingKit.itemType = ItemType.Tool;
        campingKit.rarity = ItemRarity.Common;
        campingKit.isConsumable = false;
        campingKit.searchTags = new string[] { "camping", "rest", "outdoor", "kit" };
        AssetDatabase.CreateAsset(campingKit, "Assets/Resources/Items/Miscellaneous/CampingKit.asset");
    }

    private static void EnsureFolderExists(string path)
    {
        if (!AssetDatabase.IsValidFolder(path))
        {
            var parentPath = System.IO.Path.GetDirectoryName(path);
            var folderName = System.IO.Path.GetFileName(path);
            AssetDatabase.CreateFolder(parentPath, folderName);
        }
    }
}
#endif
