using UnityEngine;
using UnityEditor;

public class CreateSampleItems
{
    [MenuItem("RPG/Create Sample Items")]
    public static void CreateItems()
    {
        // Create weapon items
        CreateSimpleSword();
        CreateSimpleBow();
        CreateIronSword();

        // Create armor items
        CreateLeatherArmor();
        CreateIronChainmail();

        // Create miscellaneous items
        CreateHealthPotion();
        CreateManaPotion();
        CreateHempRope();

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

        Debug.Log("Sample items created in Resources/Items folder!");
    }

    private static void CreateSimpleSword()
    {
        var sword = ScriptableObject.CreateInstance<WeaponItem>();
        sword.itemName = "Simple Sword";
        sword.description = "A basic sword perfect for beginners. Light and easy to handle.";
        sword.itemType = ItemType.Weapon;
        sword.rarity = ItemRarity.Common;
        sword.goldCost = 10;
        sword.minDamage = 1;
        sword.maxDamage = 6;
        sword.range = 5; // Melee range
        sword.weaponModifier = 0;
        sword.attackSpeed = 1.0f;
        sword.weaponType = WeaponType.Sword;
        sword.searchTags = new string[] { "sword", "melee", "blade", "basic", "one-handed" };

        AssetDatabase.CreateAsset(sword, "Assets/Resources/Items/Weapons/SimpleSword.asset");
    }

    private static void CreateSimpleBow()
    {
        var bow = ScriptableObject.CreateInstance<WeaponItem>();
        bow.itemName = "Simple Bow";
        bow.description = "A basic wooden bow with decent range. Perfect for hunting small game.";
        bow.itemType = ItemType.Weapon;
        bow.rarity = ItemRarity.Common;
        bow.goldCost = 15;
        bow.minDamage = 1;
        bow.maxDamage = 4;
        bow.range = 150;
        bow.weaponModifier = 0;
        bow.attackSpeed = 0.8f;
        bow.weaponType = WeaponType.Bow;
        bow.searchTags = new string[] { "bow", "ranged", "arrow", "hunting", "wood" };

        AssetDatabase.CreateAsset(bow, "Assets/Resources/Items/Weapons/SimpleBow.asset");
    }

    private static void CreateIronSword()
    {
        var sword = ScriptableObject.CreateInstance<WeaponItem>();
        sword.itemName = "Iron Sword";
        sword.description = "A well-crafted iron sword. Durable and sharp, favored by town guards.";
        sword.itemType = ItemType.Weapon;
        sword.rarity = ItemRarity.Uncommon;
        sword.goldCost = 25;
        sword.minDamage = 2;
        sword.maxDamage = 8;
        sword.range = 5;
        sword.weaponModifier = 1;
        sword.attackSpeed = 1.0f;
        sword.weaponType = WeaponType.Sword;
        sword.searchTags = new string[] { "sword", "melee", "blade", "iron", "guard", "military" };

        AssetDatabase.CreateAsset(sword, "Assets/Resources/Items/Weapons/IronSword.asset");
    }

    private static void CreateLeatherArmor()
    {
        var armor = ScriptableObject.CreateInstance<ArmorItem>();
        armor.itemName = "Leather Armor";
        armor.description = "Simple leather protection that doesn't restrict movement.";
        armor.itemType = ItemType.Armor;
        armor.rarity = ItemRarity.Common;
        armor.goldCost = 12;
        armor.armorClass = 1;
        armor.armorType = ArmorType.Light;
        armor.armorSlot = ArmorSlot.Chest;
        armor.dexterityModifier = 0; // No penalty for light armor
        armor.searchTags = new string[] { "leather", "armor", "chest", "light", "flexible" };

        AssetDatabase.CreateAsset(armor, "Assets/Resources/Items/Armor/LeatherArmor.asset");
    }

    private static void CreateIronChainmail()
    {
        var armor = ScriptableObject.CreateInstance<ArmorItem>();
        armor.itemName = "Iron Chainmail";
        armor.description = "Interlocking iron rings provide solid protection.";
        armor.itemType = ItemType.Armor;
        armor.rarity = ItemRarity.Uncommon;
        armor.goldCost = 30;
        armor.armorClass = 2;
        armor.armorType = ArmorType.Medium;
        armor.armorSlot = ArmorSlot.Chest;
        armor.dexterityModifier = -1; // Medium armor restricts movement slightly
        armor.searchTags = new string[] { "chainmail", "armor", "chest", "medium", "iron", "metal" };

        AssetDatabase.CreateAsset(armor, "Assets/Resources/Items/Armor/IronChainmail.asset");
    }

    private static void CreateHealthPotion()
    {
        var potion = ScriptableObject.CreateInstance<MiscellaneousItem>();
        potion.itemName = "Health Potion";
        potion.description = "A red liquid that restores vitality when consumed.";
        potion.itemType = ItemType.Consumable;
        potion.rarity = ItemRarity.Common;
        potion.goldCost = 3;
        potion.isConsumable = true;
        potion.isStackable = true;
        potion.maxStackSize = 10;

        // Heals 1d6+1 (2-7 health)
        potion.healthDiceCount = 1;
        potion.healthDiceType = 6;
        potion.healthBonus = 1;

        potion.searchTags = new string[] { "potion", "health", "healing", "consumable", "red" };

        AssetDatabase.CreateAsset(potion, "Assets/Resources/Items/Miscellaneous/HealthPotion.asset");
    }

    private static void CreateManaPotion()
    {
        var potion = ScriptableObject.CreateInstance<MiscellaneousItem>();
        potion.itemName = "Mana Potion";
        potion.description = "A blue liquid that restores magical energy.";
        potion.itemType = ItemType.Consumable;
        potion.rarity = ItemRarity.Common;
        potion.goldCost = 4;
        potion.isConsumable = true;
        potion.isStackable = true;
        potion.maxStackSize = 10;

        // Restores 1d4 (1-4 mana)
        potion.manaDiceCount = 1;
        potion.manaDiceType = 4;
        potion.manaBonus = 0;

        potion.searchTags = new string[] { "potion", "mana", "magic", "consumable", "blue" };

        AssetDatabase.CreateAsset(potion, "Assets/Resources/Items/Miscellaneous/ManaPotion.asset");
    }

    private static void CreateHempRope()
    {
        var rope = ScriptableObject.CreateInstance<MiscellaneousItem>();
        rope.itemName = "Hemp Rope";
        rope.description = "50 feet of sturdy rope. Essential for any adventurer.";
        rope.itemType = ItemType.Tool;
        rope.rarity = ItemRarity.Common;
        rope.silverCost = 8;
        rope.isConsumable = false;
        rope.isStackable = false;
        rope.searchTags = new string[] { "rope", "hemp", "utility", "tool", "climbing" };

        AssetDatabase.CreateAsset(rope, "Assets/Resources/Items/Miscellaneous/HempRope.asset");
    }
}
