using UnityEngine;
using System.Collections.Generic;

/// <summary>
/// Generates randomized shop names based on shop type and settlement seed
/// </summary>
public static class ShopNameGenerator
{
    private static readonly Dictionary<ShopType, (string[] prefixes, string[] suffixes)> shopNameParts =
        new Dictionary<ShopType, (string[], string[])>
        {
            [ShopType.Weapons] = (
            new string[] { "Iron", "Steel", "Sharp", "Blade", "Sword", "Battle", "War", "Forge", "Smith's", "Master" },
            new string[] { "Forge", "Armory", "Smithy", "Workshop", "Arms", "Weapons", "Blades", "Steel", "Works", "Arsenal" }
        ),
            [ShopType.Armor] = (
            new string[] { "Iron", "Steel", "Shield", "Guard", "Protection", "Battle", "Sturdy", "Strong", "Heavy", "Knight's" },
            new string[] { "Armory", "Protection", "Guards", "Shields", "Armor", "Defense", "Gear", "Works", "Smithy", "Hall" }
        ),
            [ShopType.Potions] = (
            new string[] { "Mystic", "Magic", "Healing", "Wise", "Ancient", "Bubbling", "Crystal", "Elven", "Herbal", "Sacred" },
            new string[] { "Potions", "Elixirs", "Brews", "Cauldron", "Remedies", "Apothecary", "Alchemy", "Lab", "Herbs", "Tonics" }
        ),
            [ShopType.General] = (
            new string[] { "General", "Trading", "Merchant's", "Common", "Useful", "Daily", "Town", "Village", "Traveler's", "Wanderer's" },
            new string[] { "Goods", "Store", "Supplies", "Trading Post", "Emporium", "Market", "Shop", "Merchant", "General Store", "Bazaar" }
        ),
            [ShopType.Magic] = (
            new string[] { "Arcane", "Mystical", "Enchanted", "Magical", "Wizard's", "Sorcerer's", "Ancient", "Ethereal", "Divine", "Celestial" },
            new string[] { "Emporium", "Artifacts", "Relics", "Magic", "Enchantments", "Scrolls", "Tomes", "Mysteries", "Wonders", "Sanctum" }
        ),
            [ShopType.Tools] = (
            new string[] { "Useful", "Handy", "Crafting", "Builder's", "Worker's", "Master", "Quality", "Fine", "Reliable", "Sturdy" },
            new string[] { "Tools", "Implements", "Workshop", "Supplies", "Equipment", "Gear", "Utilities", "Hardware", "Crafts", "Works" }
        )
        };

    private static readonly string[] keepersNameParts = new string[]
    {
        // Male names
        "Marcus", "Gareth", "Thorin", "Finn", "Aldric", "Bram", "Cael", "Daven", "Erik", "Galen",
        "Hector", "Ivan", "Joren", "Kane", "Lars", "Magnus", "Nolan", "Owen", "Pike", "Quinn",
        
        // Female names  
        "Elara", "Brenna", "Cira", "Delia", "Evelyn", "Fiona", "Greta", "Hilda", "Iris", "Jenna",
        "Kira", "Luna", "Mira", "Nina", "Olivia", "Piper", "Rose", "Sara", "Tara", "Vera",
        
        // Fantasy names
        "Zephyr", "Xara", "Vex", "Wick", "Yara", "Zane"
    };

    /// <summary>
    /// Generate a randomized shop name based on shop type and seed
    /// </summary>
    public static string GenerateShopName(ShopType shopType, int seed)
    {
        var random = new System.Random(seed);

        if (!shopNameParts.ContainsKey(shopType))
        {
            return $"Unknown {shopType} Shop";
        }

        var parts = shopNameParts[shopType];
        var prefix = parts.prefixes[random.Next(parts.prefixes.Length)];
        var suffix = parts.suffixes[random.Next(parts.suffixes.Length)];

        // Sometimes use "The" prefix
        var useThe = random.NextDouble() > 0.6;
        var shopName = useThe ? $"The {prefix} {suffix}" : $"{prefix} {suffix}";

        return shopName;
    }

    /// <summary>
    /// Generate a randomized shopkeeper name based on seed
    /// </summary>
    public static string GenerateShopkeeperName(int seed)
    {
        var random = new System.Random(seed);
        return keepersNameParts[random.Next(keepersNameParts.Length)];
    }

    /// <summary>
    /// Generate a shop name with embedded settlement and shop type info for uniqueness
    /// </summary>
    public static string GenerateUniqueShopName(ShopType shopType, string settlementName, Vector2Int position)
    {
        // Create a unique seed based on settlement name, position, and shop type
        var combinedSeed = settlementName.GetHashCode() ^ position.GetHashCode() ^ shopType.GetHashCode();
        return GenerateShopName(shopType, combinedSeed);
    }

    /// <summary>
    /// Generate a shopkeeper name with embedded settlement and shop type info for uniqueness
    /// </summary>
    public static string GenerateUniqueShopkeeperName(ShopType shopType, string settlementName, Vector2Int position)
    {
        // Create a unique seed based on settlement name, position, and shop type (offset for different result)
        var combinedSeed = (settlementName.GetHashCode() ^ position.GetHashCode() ^ shopType.GetHashCode()) + 12345;
        return GenerateShopkeeperName(combinedSeed);
    }
}
