using UnityEngine;
using UnityEngine.UIElements; // Make sure you have this using directive
using UnityEngine.SceneManagement; // Added for scene management
using System.Collections.Generic; // Needed for List<>
using System.Linq; // Needed for LINQ operations
using System.Collections; // Needed for coroutines

public class MainTeamSelectScript : MonoBehaviour
{
    private UIDocument uiDocument;
    private TeamCharacter currentCharacter;
    private List<Button> characterSlotButtons = new List<Button>();
    private List<Button> createNewCharacterButtons = new List<Button>();

    // Character slot management
    private TeamCharacter[] characterSlots = new TeamCharacter[4]; // 4 character slots
    private int currentSlotIndex = -1; // -1 means no slot selected

    // UI elements
    private TextField characterNameField;
    private Button randomizeNameButton;
    private DropdownField genderDropdown;
    private IntegerField strengthField, dexterityField, constitutionField, wisdomField;
    private IntegerField initField, damageField, spellCastingField, movementSpeedField, hpField, acField;

    // Attribute management UI
    private Button randomizeAttributesButton;
    private Button resetToPointBuyButton;
    private Label availablePointsLabel;
    private Label creationModeLabel;

    // Inventory/Shop UI elements
    private Button addWeaponButton;
    private Button addArmorButton;
    private Button addMiscButton;
    private IntegerField goldField;
    private IntegerField silverField;
    private IntegerField copperField;
    private VisualElement weaponsList;
    private VisualElement armorList;
    private VisualElement miscList;

    // Shop reference
    private SimpleShopManager shopManager;

    // Navigation buttons
    private Button backToTitleButton;
    private Button proceedToBattleButton;

    void Awake()
    {
        uiDocument = GetComponent<UIDocument>();
        if (uiDocument == null)
        {
            Debug.LogError("UIDocument component not found on this GameObject.", this);
            return; // Exit if no UIDocument is found
        }

        // Initialize character slots array
        characterSlots = new TeamCharacter[4];

        // Clear all existing character equipment to ensure shop-only acquisition
        ClearAllCharacterEquipment();

        // Start with no character selected (empty character sheet)
        currentCharacter = null;
        currentSlotIndex = -1;

        // Check if team has been finalized (proceeded to map)
        bool teamFinalized = IsTeamFinalized();

        // Load saved team if available
        LoadSavedTeamData();

        // Get UI element references
        GetUIElementReferences();

        // Set up event listeners
        SetupEventListeners();

        // Try to find missing UI elements one more time after everything is set up
        ValidateUIElements();

        // If team is finalized, disable editing and show message
        if (teamFinalized)
        {
            SetTeamFinalizedMode();
        }

        // Initialize UI with character data
        UpdateCharacterUI();
        UpdateSlotVisualStates();
        UpdateNavigationButtons();
    }

    private void ValidateUIElements()
    {
        // Final attempt to find critical UI elements that might have been missed
        if (proceedToBattleButton == null && uiDocument != null)
        {
            var root = uiDocument.rootVisualElement;
            Debug.LogWarning("Final attempt to find proceedToBattleButton...");

            // Try searching with different approaches
            proceedToBattleButton = root.Q<Button>("ProceedToBattleButton") ??
                                  root.Q<Button>("ProceedButton") ??
                                  root.Q<Button>("StartButton") ??
                                  root.Q<Button>(className: "proceed-button");

            // If still not found, search through all buttons for text matches
            if (proceedToBattleButton == null)
            {
                var allButtons = root.Query<Button>().ToList();
                foreach (var button in allButtons)
                {
                    if (button.text.ToLower().Contains("proceed") ||
                        button.text.ToLower().Contains("start") ||
                        button.text.ToLower().Contains("battle"))
                    {
                        proceedToBattleButton = button;

                        break;
                    }
                }
            }

            if (proceedToBattleButton != null)
            {

                // Set up the event listener since we found it late
                proceedToBattleButton.clicked += OnProceedToBattleClicked;
            }
            else
            {
                Debug.LogError("CRITICAL: Could not find proceed button in UI! Please check the UI Document.");
            }
        }
    }

    private void GetUIElementReferences()
    {
        var root = uiDocument.rootVisualElement;

        // Character info fields
        characterNameField = root.Q<TextField>("CharacterNameField");
        randomizeNameButton = root.Q<Button>("RandomizeNameButton");
        genderDropdown = root.Q<DropdownField>("GenderDropdown");

        // Stat fields
        strengthField = root.Q<IntegerField>("StrengthValue");
        dexterityField = root.Q<IntegerField>("DexterityValue");
        constitutionField = root.Q<IntegerField>("ConstitutionValue");
        wisdomField = root.Q<IntegerField>("WisdomValue");

        // Derived stat fields
        initField = root.Q<IntegerField>("InitValue");
        damageField = root.Q<IntegerField>("DamageValue");
        spellCastingField = root.Q<IntegerField>("SpellCastingValue");
        movementSpeedField = root.Q<IntegerField>("MovementSpeedValue");
        hpField = root.Q<IntegerField>("HPValue");
        acField = root.Q<IntegerField>("ACValue");

        // Attribute management UI
        randomizeAttributesButton = root.Q<Button>("RandomizeAttributesButton");
        resetToPointBuyButton = root.Q<Button>("ResetToPointBuyButton");
        availablePointsLabel = root.Q<Label>("AvailablePointsLabel");
        creationModeLabel = root.Q<Label>("CreationModeLabel");

        // Find all buttons with the name "CreateNewCharacterButton"
        createNewCharacterButtons = uiDocument.rootVisualElement.Query<Button>("CreateNewCharacterButton").ToList();

        // Find all ViewCharacterButton buttons
        characterSlotButtons = uiDocument.rootVisualElement.Query<Button>("ViewCharacterButton").ToList();

        // Inventory/Shop UI elements
        addWeaponButton = root.Q<Button>("AddWeaponButton");
        addArmorButton = root.Q<Button>("AddArmourButton");
        addMiscButton = root.Q<Button>("AddMiscButton");
        goldField = root.Q<IntegerField>("GoldValue");
        silverField = root.Q<IntegerField>("SilverValue");
        copperField = root.Q<IntegerField>("CopperValue");
        weaponsList = root.Q<VisualElement>("WeaponsList");
        armorList = root.Q<VisualElement>("ArmourList");
        miscList = root.Q<VisualElement>("MiscList");

        // Navigation buttons
        backToTitleButton = root.Q<Button>("BackToTitleButton");
        proceedToBattleButton = root.Q<Button>("ProceedToBattleButton");

        // Debug and fallback for Back to Title button
        if (backToTitleButton == null)
        {
            Debug.LogWarning("BackToTitleButton not found! Creating fallback button...");

            // Create a simple fallback button
            backToTitleButton = new Button();
            backToTitleButton.text = "Back to Title";
            backToTitleButton.name = "BackToTitleButton_Fallback";
            backToTitleButton.style.position = Position.Absolute;
            backToTitleButton.style.top = 10;
            backToTitleButton.style.left = 10;
            backToTitleButton.style.width = 120;
            backToTitleButton.style.height = 30;
            root.Add(backToTitleButton);

        }

        // Find shop manager in scene
        shopManager = FindFirstObjectByType<SimpleShopManager>();
        if (shopManager == null)
        {
            Debug.LogWarning("No SimpleShopManager found in scene. Shop functionality will be limited.");
            Debug.LogWarning("Trying to refresh shop manager reference...");

            // Try again after a short delay to allow for potential late initialization
            StartCoroutine(RefreshShopManagerReference());
        }
        else
        {

            SetupShopManagerCallbacks();
        }

        // Check if any buttons were found
        if (createNewCharacterButtons.Count == 0)
        {
            Debug.LogWarning("No buttons with the name 'CreateNewCharacterButton' found in the UI Document.");
        }
        else
        {
            // Add a click listener to each button found
            foreach (Button button in createNewCharacterButtons)
            {
                // Example of passing data (requires adjusting the handler method):
                int characterIndex = createNewCharacterButtons.IndexOf(button); // Example data
                button.clicked += () => OnCreateNewCharacterButtonClicked(characterIndex);
            }

        }

        // Add click listeners to ViewCharacterButton buttons
        for (int i = 0; i < characterSlotButtons.Count; i++)
        {
            int index = i; // Capture the index for the closure
            characterSlotButtons[i].clicked += () => OnViewCharacterButtonClicked(index);
        }
    }

    private void SetupEventListeners()
    {
        // Name randomization
        if (randomizeNameButton != null)
        {
            randomizeNameButton.clicked += OnRandomizeNameClicked;
        }

        // Character name change
        if (characterNameField != null)
        {
            characterNameField.RegisterValueChangedCallback(OnCharacterNameChanged);
        }

        // Gender change
        if (genderDropdown != null)
        {
            genderDropdown.RegisterValueChangedCallback(OnGenderChanged);
        }

        // Stat changes
        if (strengthField != null)
            strengthField.RegisterValueChangedCallback(evt => OnStatChanged());
        if (dexterityField != null)
            dexterityField.RegisterValueChangedCallback(evt => OnStatChanged());
        if (constitutionField != null)
            constitutionField.RegisterValueChangedCallback(evt => OnStatChanged());
        if (wisdomField != null)
            wisdomField.RegisterValueChangedCallback(evt => OnStatChanged());

        // Attribute management
        if (randomizeAttributesButton != null)
            randomizeAttributesButton.clicked += OnRandomizeAttributesClicked;
        if (resetToPointBuyButton != null)
            resetToPointBuyButton.clicked += OnResetToPointBuyClicked;

        // Inventory/Shop management
        if (addWeaponButton != null)
            addWeaponButton.clicked += OnAddWeaponClicked;
        if (addArmorButton != null)
            addArmorButton.clicked += OnAddArmorClicked;
        if (addMiscButton != null)
            addMiscButton.clicked += OnAddMiscClicked;

        // Bank field changes
        if (goldField != null)
            goldField.RegisterValueChangedCallback(evt => OnBankChanged());
        if (silverField != null)
            silverField.RegisterValueChangedCallback(evt => OnBankChanged());
        if (copperField != null)
            copperField.RegisterValueChangedCallback(evt => OnBankChanged());

        // Navigation buttons
        if (backToTitleButton != null)
            backToTitleButton.clicked += OnBackToTitleClicked;
        if (proceedToBattleButton != null)
            proceedToBattleButton.clicked += OnProceedToBattleClicked;
        else
            Debug.LogWarning("proceedToBattleButton is null during SetupEventListeners - will try to set up event listener later");
    }

    private void OnRandomizeNameClicked()
    {
        if (currentCharacter == null) return;

        currentCharacter.GenerateRandomName();
        characterNameField.value = currentCharacter.name;

        // Update the slot button and save to slot
        UpdateCurrentSlotButton();
        SaveCurrentCharacterToSlot();
    }

    private void OnCharacterNameChanged(ChangeEvent<string> evt)
    {
        if (currentCharacter == null) return;

        currentCharacter.name = evt.newValue;

        // Update the slot button and save to slot
        UpdateCurrentSlotButton();
        SaveCurrentCharacterToSlot();

        // Update the character card if we have a current slot
        if (currentSlotIndex >= 0)
        {
            UpdateCharacterCard(currentSlotIndex, currentCharacter);
        }
    }

    private void OnGenderChanged(ChangeEvent<string> evt)
    {
        if (currentCharacter == null) return;

        currentCharacter.SetGender(evt.newValue == "Male");
        // Optionally regenerate name when gender changes
        // You can remove this if you don't want automatic name regeneration
        currentCharacter.GenerateRandomName();
        characterNameField.value = currentCharacter.name;

        // Update the slot button and save to slot
        UpdateCurrentSlotButton();
        SaveCurrentCharacterToSlot();

        // Update the character card if we have a current slot
        if (currentSlotIndex >= 0)
        {
            UpdateCharacterCard(currentSlotIndex, currentCharacter);
        }
    }

    private void OnStatChanged()
    {
        if (currentCharacter == null) return;

        // Validate point-buy constraints
        int newStr = strengthField?.value ?? currentCharacter.strength;
        int newDex = dexterityField?.value ?? currentCharacter.dexterity;
        int newCon = constitutionField?.value ?? currentCharacter.constitution;
        int newWis = wisdomField?.value ?? currentCharacter.wisdom;

        // Clamp values to valid range (4-18)
        newStr = Mathf.Clamp(newStr, 4, 18);
        newDex = Mathf.Clamp(newDex, 4, 18);
        newCon = Mathf.Clamp(newCon, 4, 18);
        newWis = Mathf.Clamp(newWis, 4, 18);

        // Check if the new combination is affordable
        int totalCost = TeamCharacter.GetStatCost(newStr) + TeamCharacter.GetStatCost(newDex) +
                       TeamCharacter.GetStatCost(newCon) + TeamCharacter.GetStatCost(newWis);

        if (totalCost <= 18) // Changed from 27 to 18 for 4-stat system
        {
            // Update character stats from UI
            currentCharacter.strength = newStr;
            currentCharacter.dexterity = newDex;
            currentCharacter.constitution = newCon;
            currentCharacter.wisdom = newWis;
            currentCharacter.UpdateAvailablePoints();
        }
        else
        {
            // Reset to previous valid values
            if (strengthField != null) strengthField.value = currentCharacter.strength;
            if (dexterityField != null) dexterityField.value = currentCharacter.dexterity;
            if (constitutionField != null) constitutionField.value = currentCharacter.constitution;
            if (wisdomField != null) wisdomField.value = currentCharacter.wisdom;
        }

        // Update derived stats and point display
        UpdateDerivedStats();
        UpdateAttributeUI();
        SaveCurrentCharacterToSlot();
        UpdateNavigationButtons();

        // Update the character card if we have a current slot
        if (currentSlotIndex >= 0)
        {
            UpdateCharacterCard(currentSlotIndex, currentCharacter);
        }
    }
    private void OnRandomizeAttributesClicked()
    {
        if (currentCharacter == null) return;

        currentCharacter.RandomizeAttributes();
        UpdateCharacterUI();
        UpdateAttributeUI();
        SaveCurrentCharacterToSlot();
        UpdateNavigationButtons();

        // Update the character card if we have a current slot
        if (currentSlotIndex >= 0)
        {
            UpdateCharacterCard(currentSlotIndex, currentCharacter);
        }
    }

    private void OnResetToPointBuyClicked()
    {
        if (currentCharacter == null) return;

        currentCharacter.ResetToPointBuy();
        UpdateCharacterUI();
        UpdateAttributeUI();
        SaveCurrentCharacterToSlot();
        UpdateNavigationButtons();

        // Update the character card if we have a current slot
        if (currentSlotIndex >= 0)
        {
            UpdateCharacterCard(currentSlotIndex, currentCharacter);
        }
    }

    private void OnAddWeaponClicked()
    {
        if (currentCharacter == null)
        {
            Debug.LogWarning("OnAddWeaponClicked: No current character selected");
            return;
        }

        // Open weapon shop for this character
        OpenWeaponShop();
    }

    private void OpenWeaponShop()
    {
        if (shopManager != null)
        {
            shopManager.OpenShop(currentCharacter);
        }
        else
        {
            Debug.LogWarning("No shop manager found - cannot open shop");
        }
    }

    private void AddWeaponToCharacter(string weaponType)
    {
        // Add weapon to character's inventory (would integrate with real shop/inventory system)
        if (currentCharacter.weapons == null)
        {
            currentCharacter.weapons = new System.Collections.Generic.List<string>();
        }

        currentCharacter.weapons.Add(weaponType);
        SaveCurrentCharacterToSlot();
    }

    private void OnAddArmorClicked()
    {
        if (currentCharacter == null) return;

        if (shopManager != null)
        {
            shopManager.OpenShop(currentCharacter);
        }
        else
        {
            Debug.LogWarning("No shop manager found - cannot open shop");
        }
    }

    private void OnAddMiscClicked()
    {
        if (currentCharacter == null) return;

        if (shopManager != null)
        {
            shopManager.OpenShop(currentCharacter);
        }
        else
        {
            Debug.LogWarning("No shop manager found - cannot open shop");
        }
    }

    private void AddArmorToCharacter(string armorType)
    {
        if (currentCharacter.armor == null)
        {
            currentCharacter.armor = new System.Collections.Generic.List<string>();
        }

        currentCharacter.armor.Add(armorType);
        SaveCurrentCharacterToSlot();
        UpdateInventoryUI();
    }

    private void AddMiscItemToCharacter(string itemType)
    {
        if (currentCharacter.miscItems == null)
        {
            currentCharacter.miscItems = new System.Collections.Generic.List<string>();
        }

        currentCharacter.miscItems.Add(itemType);
        SaveCurrentCharacterToSlot();
        UpdateInventoryUI();
    }

    private void OnBankChanged()
    {
        if (currentCharacter == null) return;

        currentCharacter.gold = goldField?.value ?? currentCharacter.gold;
        currentCharacter.silver = silverField?.value ?? currentCharacter.silver;
        currentCharacter.copper = copperField?.value ?? currentCharacter.copper;

        SaveCurrentCharacterToSlot();
    }

    private void UpdateInventoryUI()
    {
        if (currentCharacter == null) return;

        // Update bank fields
        if (goldField != null) goldField.value = currentCharacter.gold;
        if (silverField != null) silverField.value = currentCharacter.silver;
        if (copperField != null) copperField.value = currentCharacter.copper;

        // Update weapon list
        UpdateItemList(weaponsList, currentCharacter.weapons, "WeaponItemRow", OnRemoveWeapon);

        // Update armor list  
        UpdateItemList(armorList, currentCharacter.armor, "ArmorItemRow", OnRemoveArmor);

        // Update misc list
        UpdateItemList(miscList, currentCharacter.miscItems, "MiscItemRow", OnRemoveMisc);
    }

    private void UpdateItemList(VisualElement container, System.Collections.Generic.List<string> items, string rowClassName, System.Action<string> removeAction)
    {
        if (container == null || items == null) return;

        // Clear existing items (except example rows)
        var itemsToRemove = container.Query<VisualElement>(className: "inventory-item-row").ToList()
            .Where(item => !item.name.Contains("Example")).ToList();

        foreach (var item in itemsToRemove)
        {
            container.Remove(item);
        }

        // Add current items
        foreach (var itemName in items)
        {
            var itemRow = new VisualElement();
            itemRow.AddToClassList("inventory-item-row");
            itemRow.AddToClassList(rowClassName);

            var nameLabel = new Label(itemName);
            nameLabel.AddToClassList("item-name-label");
            itemRow.Add(nameLabel);

            var removeButton = new Button(() => removeAction(itemName));
            removeButton.text = "Remove (Refund)";
            removeButton.AddToClassList("remove-item-button");

            // Add tooltip or title to show refund information
            string itemType = GetItemTypeFromClassName(rowClassName);
            string refundInfo = GetRefundInfo(itemName, itemType);
            if (!string.IsNullOrEmpty(refundInfo))
            {
                removeButton.tooltip = $"Remove and refund: {refundInfo}";
            }

            itemRow.Add(removeButton);

            container.Add(itemRow);
        }
    }

    private string GetItemTypeFromClassName(string className)
    {
        if (className.Contains("Weapon")) return "weapon";
        if (className.Contains("Armor")) return "armor";
        if (className.Contains("Misc")) return "misc";
        return "";
    }

    private string GetRefundInfo(string itemName, string itemType)
    {
        if (shopManager == null) return "";

        System.Collections.Generic.List<SimpleShopItem> itemList = null;

        switch (itemType.ToLower())
        {
            case "weapon":
                itemList = shopManager.weapons;
                break;
            case "armor":
                itemList = shopManager.armor;
                break;
            case "misc":
                itemList = shopManager.miscItems;
                break;
        }

        if (itemList != null)
        {
            var shopItem = itemList.Find(item => item.name == itemName);
            if (shopItem != null)
            {
                return shopItem.GetCostString();
            }
        }

        return "";
    }

    private void RefundItemPrice(string itemName, string itemType)
    {
        if (shopManager == null || currentCharacter == null)
        {
            Debug.LogWarning($"Cannot refund {itemName}: shop manager or current character is null");
            return;
        }

        // Find the item price from the shop manager
        int goldRefund = 0;
        int silverRefund = 0;
        int copperRefund = 0;

        // Check the appropriate list based on item type
        System.Collections.Generic.List<SimpleShopItem> itemList = null;

        switch (itemType.ToLower())
        {
            case "weapon":
                itemList = shopManager.weapons;
                break;
            case "armor":
                itemList = shopManager.armor;
                break;
            case "misc":
                itemList = shopManager.miscItems;
                break;
        }

        if (itemList != null)
        {
            // Find the item in the shop list
            var shopItem = itemList.Find(item => item.name == itemName);
            if (shopItem != null)
            {
                goldRefund = shopItem.goldCost;
                silverRefund = shopItem.silverCost;
                copperRefund = shopItem.copperCost;

                // Add the refund to the character's money
                currentCharacter.gold += goldRefund;
                currentCharacter.silver += silverRefund;
                currentCharacter.copper += copperRefund;
            }
            else
            {
                Debug.LogWarning($"Could not find {itemName} in shop {itemType} list for refund");
            }
        }
        else
        {
            Debug.LogWarning($"Could not access shop {itemType} list for refund");
        }
    }

    private void OnRemoveWeapon(string weaponName)
    {
        if (currentCharacter?.weapons != null)
        {
            currentCharacter.weapons.Remove(weaponName);

            // Refund the weapon price
            RefundItemPrice(weaponName, "weapon");

            SaveCurrentCharacterToSlot();
            UpdateInventoryUI();
        }
    }

    private void OnRemoveArmor(string armorName)
    {
        if (currentCharacter?.armor != null)
        {
            currentCharacter.armor.Remove(armorName);

            // Refund the armor price
            RefundItemPrice(armorName, "armor");

            SaveCurrentCharacterToSlot();
            UpdateInventoryUI();
        }
    }

    private void OnRemoveMisc(string itemName)
    {
        if (currentCharacter?.miscItems != null)
        {
            currentCharacter.miscItems.Remove(itemName);

            // Refund the misc item price
            RefundItemPrice(itemName, "misc");

            SaveCurrentCharacterToSlot();
            UpdateInventoryUI();
        }
    }

    private void UpdateCharacterUI()
    {
        if (currentCharacter == null)
        {
            // Clear the UI when no character is selected
            if (characterNameField != null) characterNameField.value = "";
            if (genderDropdown != null) genderDropdown.value = "Male";
            if (strengthField != null) strengthField.value = 10;
            if (dexterityField != null) dexterityField.value = 10;
            if (constitutionField != null) constitutionField.value = 10;
            if (wisdomField != null) wisdomField.value = 10;

            // Clear derived stats
            if (initField != null) initField.value = 0;
            if (damageField != null) damageField.value = 0;
            if (spellCastingField != null) spellCastingField.value = 0;
            if (movementSpeedField != null) movementSpeedField.value = 30;
            if (hpField != null) hpField.value = 10;
            if (acField != null) acField.value = 0;

            // Clear attribute UI
            if (creationModeLabel != null) creationModeLabel.text = "Mode: None";
            if (availablePointsLabel != null) availablePointsLabel.text = "Available Points: 0";
            return;
        }

        // Update UI with current character data
        if (characterNameField != null)
            characterNameField.value = currentCharacter.name;

        if (genderDropdown != null)
            genderDropdown.value = currentCharacter.isMale ? "Male" : "Female";

        if (strengthField != null) strengthField.value = currentCharacter.strength;
        if (dexterityField != null) dexterityField.value = currentCharacter.dexterity;
        if (constitutionField != null) constitutionField.value = currentCharacter.constitution;
        if (wisdomField != null) wisdomField.value = currentCharacter.wisdom;

        // Recalculate equipment bonuses from inventory
        currentCharacter.RecalculateEquipmentBonuses();

        UpdateDerivedStats();
        UpdateAttributeUI();
        UpdateInventoryUI();
    }

    private void UpdateAttributeUI()
    {
        if (currentCharacter == null) return;

        // Update creation mode display
        if (creationModeLabel != null)
        {
            creationModeLabel.text = "Mode: Point Buy";
        }

        // Update available points display
        if (availablePointsLabel != null)
        {
            availablePointsLabel.text = $"Available Points: {currentCharacter.availablePoints}";
        }

        // All stat fields are always editable in the new system
        if (strengthField != null) strengthField.SetEnabled(true);
        if (dexterityField != null) dexterityField.SetEnabled(true);
        if (constitutionField != null) constitutionField.SetEnabled(true);
        if (wisdomField != null) wisdomField.SetEnabled(true);
    }

    private void UpdateDerivedStats()
    {
        if (currentCharacter == null) return;

        // Update the read-only derived stats (uses final stats including equipment modifiers)
        if (initField != null) initField.value = currentCharacter.Initiative;
        if (damageField != null) damageField.value = currentCharacter.DamageBonus;
        if (spellCastingField != null) spellCastingField.value = currentCharacter.SpellAC;
        if (movementSpeedField != null) movementSpeedField.value = currentCharacter.MovementSpeed;
        if (hpField != null) hpField.value = currentCharacter.HitPoints;
        if (acField != null) acField.value = currentCharacter.ArmorClass;
    }

    private void UpdateCurrentSlotButton()
    {
        // Update the character card information if we have a current slot selected
        if (currentSlotIndex >= 0 && currentSlotIndex < characterSlotButtons.Count && currentCharacter != null)
        {
            UpdateCharacterCard(currentSlotIndex, currentCharacter);
        }
    }

    private void UpdateCharacterCard(int slotIndex, TeamCharacter character)
    {
        if (slotIndex < 0 || slotIndex >= 4 || character == null) return;

        var root = uiDocument.rootVisualElement;

        // Find the character slot instance based on index
        string[] slotNames = { "Character1", "Character2", "Character3", "Character4" };
        var characterSlot = root.Q<VisualElement>(slotNames[slotIndex]);

        if (characterSlot == null) return;

        // Find the character card button within this slot
        var characterCard = characterSlot.Q<Button>("ViewCharacterButton");

        if (characterCard == null) return;

        // Update character name
        var nameLabel = characterCard.Q<Label>("CharacterNameLabel");
        if (nameLabel != null)
        {
            nameLabel.text = character.name;
        }

        // Update stats
        var strengthLabel = characterCard.Q<Label>("StrengthLabel");
        if (strengthLabel != null) strengthLabel.text = character.strength.ToString();

        var dexterityLabel = characterCard.Q<Label>("DexterityLabel");
        if (dexterityLabel != null) dexterityLabel.text = character.dexterity.ToString();

        var constitutionLabel = characterCard.Q<Label>("ConstitutionLabel");
        if (constitutionLabel != null) constitutionLabel.text = character.constitution.ToString();

        var wisdomLabel = characterCard.Q<Label>("WisdomLabel");
        if (wisdomLabel != null) wisdomLabel.text = character.wisdom.ToString();

        // Update HP and AC
        var hpLabel = characterCard.Q<Label>("HPLabel");
        if (hpLabel != null) hpLabel.text = $"HP: {character.HitPoints}";

        var acLabel = characterCard.Q<Label>("ACLabel");
        if (acLabel != null) acLabel.text = $"AC: {character.ArmorClass}";

        // Show the character card and hide the create button
        var createButton = characterSlot.Q<Button>("CreateNewCharacterButton");
        if (createButton != null)
        {
            createButton.style.display = DisplayStyle.None;
        }

        characterCard.RemoveFromClassList("disabled-card");
        characterCard.style.display = DisplayStyle.Flex;
        characterCard.SetEnabled(true);
    }

    private void SaveCurrentCharacterToSlot()
    {
        // Save the current character data back to its slot
        if (currentSlotIndex >= 0 && currentSlotIndex < characterSlots.Length && currentCharacter != null)
        {
            characterSlots[currentSlotIndex] = currentCharacter.CreateCopy();
        }
    }

    private void UpdateSlotVisualStates()
    {
        var root = uiDocument.rootVisualElement;
        string[] slotNames = { "Character1", "Character2", "Character3", "Character4" };

        // Update visual states of all character slots
        for (int i = 0; i < slotNames.Length; i++)
        {
            var characterSlot = root.Q<VisualElement>(slotNames[i]);
            if (characterSlot == null) continue;

            // Remove all visual state classes first
            characterSlot.RemoveFromClassList("selected-slot");

            // Add selected state if this is the current slot
            if (i == currentSlotIndex)
            {
                characterSlot.AddToClassList("selected-slot");
            }

            // Update character card visibility and data
            if (characterSlots[i] != null)
            {
                // Character exists in this slot
                UpdateCharacterCard(i, characterSlots[i]);
            }
            else
            {
                // No character in this slot - show create button
                var createButton = characterSlot.Q<Button>("CreateNewCharacterButton");
                var characterCard = characterSlot.Q<Button>("ViewCharacterButton");

                if (createButton != null)
                {
                    createButton.style.display = DisplayStyle.Flex;
                }

                if (characterCard != null)
                {
                    characterCard.style.display = DisplayStyle.None;
                    characterCard.AddToClassList("disabled-card");
                    characterCard.SetEnabled(false);
                }
            }
        }
    }

    // Example handler if you need to pass data (like an index)
    private void OnCreateNewCharacterButtonClicked(int index)
    {
        // Create a new character with random name
        currentCharacter = new TeamCharacter();
        // Generate name after construction to avoid serialization issues
        currentCharacter.GenerateRandomName();

        // Save character to the slot (create a proper copy)
        characterSlots[index] = currentCharacter.CreateCopy();
        currentSlotIndex = index;



        UpdateCharacterUI();
        UpdateSlotVisualStates();

        // Update navigation buttons immediately after character creation
        UpdateNavigationButtons();

        // Update the character card for this slot
        UpdateCharacterCard(index, currentCharacter);

        // Call UpdateNavigationButtons again after UI updates
        UpdateNavigationButtons();
    }

    private void OnViewCharacterButtonClicked(int index)
    {
        // Check if there's a character in this slot
        if (characterSlots[index] != null)
        {
            // Create a working copy of the character from the slot
            currentCharacter = characterSlots[index].CreateCopy();
            currentSlotIndex = index;
            UpdateCharacterUI();
            UpdateSlotVisualStates();
            UpdateNavigationButtons();
        }
        else
        {
            // Slot is empty, save current character to this slot if we have one
            if (currentCharacter != null)
            {
                // Create a proper copy of the current character data
                characterSlots[index] = currentCharacter.CreateCopy();
                currentSlotIndex = index;

                // Update the character card for this slot
                UpdateCharacterCard(index, currentCharacter);

                UpdateSlotVisualStates();
                UpdateNavigationButtons();
            }
        }
    }

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        // You can keep Start if needed for other initialization
    }

    // Update is called once per frame
    void Update()
    {
        // Debug keys for testing
        if (Input.GetKeyDown(KeyCode.F5))
        {

            DebugCurrentCharacterSlots();
        }

        if (Input.GetKeyDown(KeyCode.F6))
        {

            SaveTeamToGameState();
        }

        if (Input.GetKeyDown(KeyCode.F7))
        {

            TestPlayerPrefsData();
        }
    }

    private void DebugCurrentCharacterSlots()
    {

        for (int i = 0; i < characterSlots.Length; i++)
        {
            if (characterSlots[i] != null)
            {
                var character = characterSlots[i];

            }
            else
            {

            }
        }

    }

    private void TestPlayerPrefsData()
    {

        for (int i = 0; i < 4; i++)
        {
            string existsKey = $"Character{i}_Exists";
            if (PlayerPrefs.HasKey(existsKey))
            {
                int exists = PlayerPrefs.GetInt(existsKey);


                if (exists == 1)
                {
                    string prefix = $"Character{i}_";
                    string name = PlayerPrefs.GetString(prefix + "Name", "UNKNOWN");
                    int str = PlayerPrefs.GetInt(prefix + "Strength", 0);
                    int dex = PlayerPrefs.GetInt(prefix + "Dexterity", 0);
                    int con = PlayerPrefs.GetInt(prefix + "Constitution", 0);
                    int wis = PlayerPrefs.GetInt(prefix + "Wisdom", 0);

                }
            }
            else
            {

            }
        }

    }

    void OnDestroy()
    {
        // Clean up shop manager callbacks to prevent memory leaks
        if (shopManager != null)
        {
            shopManager.OnCharacterDataChanged -= OnShopCharacterDataChanged;
        }
    }

    #region Navigation Methods

    private void OnBackToTitleClicked()
    {
        // Check if team is finalized and this is acting as "New Game" button
        if (IsTeamFinalized())
        {
            // Show confirmation dialog for starting new game
#if UNITY_EDITOR
            bool confirmed = UnityEditor.EditorUtility.DisplayDialog(
                "Start New Game",
                "This will start a completely new game and overwrite your current progress.\n\nAre you sure you want to continue?",
                "Start New Game",
                "Cancel"
            );

            if (confirmed)
            {
                StartNewGame();
            }
#else
            // In builds, we'll assume user confirms for now
            // In a real game, implement a proper UI dialog system
            Debug.LogWarning("Starting new game - implement proper dialog for builds");
            StartNewGame();
#endif
        }
        else
        {
            // Normal back to title behavior

            SceneManager.LoadScene("TitleScreenScene");
        }
    }

    private void StartNewGame()
    {


        // Initialize GameStateManager for new game
        if (GameStateManager.Instance != null)
        {
            GameStateManager.Instance.StartNewGame();
        }

        // Clear all save data
        ClearAllSaveData();

        // Reload the scene to start fresh
        SceneManager.LoadScene("MainTeamSelectScene");
    }

    private void ClearAllSaveData()
    {
        // Clear GameStateManager data
        if (GameStateManager.Instance != null)
        {
            GameStateManager.Instance.teamSetupComplete = false;
            GameStateManager.Instance.hasGeneratedMap = false;
        }

        // Clear PlayerPrefs data
        for (int i = 0; i < 4; i++)
        {
            string prefix = $"Character{i}_";
            PlayerPrefs.DeleteKey($"Character{i}_Exists");
            PlayerPrefs.DeleteKey(prefix + "Name");
            PlayerPrefs.DeleteKey(prefix + "IsMale");
            PlayerPrefs.DeleteKey(prefix + "Strength");
            PlayerPrefs.DeleteKey(prefix + "Dexterity");
            PlayerPrefs.DeleteKey(prefix + "Constitution");
            PlayerPrefs.DeleteKey(prefix + "Wisdom");
            PlayerPrefs.DeleteKey(prefix + "Gold");
            PlayerPrefs.DeleteKey(prefix + "Silver");
            PlayerPrefs.DeleteKey(prefix + "Copper");
        }

        // Clear map data
        PlayerPrefs.DeleteKey("HasGeneratedMap");
        PlayerPrefs.DeleteKey("MapSeed");
        PlayerPrefs.DeleteKey("TeamSize");
        PlayerPrefs.DeleteKey("TeamSetupComplete");
        PlayerPrefs.DeleteKey("GameSaved");

        PlayerPrefs.Save();

    }

    private void OnProceedToBattleClicked()
    {
        // Check if team is already finalized
        if (IsTeamFinalized())
        {
            // Team is finalized, return to MapScene2

            SceneManager.LoadScene("MapScene2");
            return;
        }

        // Check if we have at least one character
        if (!HasAtLeastOneCharacter())
        {
            Debug.LogWarning("Need at least one character to proceed to battle!");
            // TODO: Show message to user
            return;
        }

        // Save all configured characters before proceeding
        SaveTeamToGameState();

        // Mark that the team has proceeded to the map (finalized)
        // Save to both GameStateManager and PlayerPrefs for redundancy
        if (GameStateManager.Instance != null)
        {
            GameStateManager.Instance.teamSetupComplete = true;
            GameStateManager.Instance.SaveGame();
        }

        // Also save to PlayerPrefs as backup
        PlayerPrefs.SetInt("TeamSetupComplete", 1);
        PlayerPrefs.Save();



        SceneManager.LoadScene("MapScene2");
    }

    private void SaveTeamToGameState()
    {


        // Save team data to GameStateManager for persistence
        List<TeamCharacter> configuredCharacters = new List<TeamCharacter>();

        for (int i = 0; i < characterSlots.Length; i++)
        {
            if (characterSlots[i] != null)
            {
                configuredCharacters.Add(characterSlots[i]);
                // Save individual character data
                SaveCharacterToPlayerPrefs(characterSlots[i], i);




                // Verify the save immediately
                PlayerPrefs.SetInt($"Character{i}_Exists", 1);
                string testName = PlayerPrefs.GetString($"Character{i}_Name", "NOT_FOUND");

            }
            else
            {
                // Clear any existing character data for empty slots
                PlayerPrefs.DeleteKey($"Character{i}_Exists");

            }
        }



        // Save team composition info
        PlayerPrefs.SetInt("TeamSize", configuredCharacters.Count);


        // Update GameStateManager if available
        if (GameStateManager.Instance != null)
        {

            // Copy our team to the GameStateManager
            for (int i = 0; i < characterSlots.Length; i++)
            {
                if (characterSlots[i] != null)
                {
                    GameStateManager.Instance.savedTeam[i] = characterSlots[i].CreateCopy();

                }
                else
                {
                    GameStateManager.Instance.savedTeam[i] = null;
                }
            }
            GameStateManager.Instance.teamSetupComplete = true;
            GameStateManager.Instance.SaveGame();

        }
        else
        {
            Debug.LogWarning("GameStateManager.Instance is null!");
        }

        // Log configured team for MapScene


        foreach (var character in configuredCharacters)
        {

        }


        PlayerPrefs.Save(); // Ensure data is written to disk


    }

    public List<TeamCharacter> GetConfiguredCharacters()
    {
        // Public method to get all configured characters (useful for other scripts)
        List<TeamCharacter> configuredCharacters = new List<TeamCharacter>();

        for (int i = 0; i < characterSlots.Length; i++)
        {
            if (characterSlots[i] != null)
            {
                configuredCharacters.Add(characterSlots[i]);
            }
        }

        return configuredCharacters;
    }

    private void UpdateNavigationButtons()
    {
        // Enable/disable proceed button based on team status
        if (proceedToBattleButton != null)
        {
            bool canProceed = HasAtLeastOneCharacter();


            proceedToBattleButton.SetEnabled(canProceed);

            // Also remove/add visual disabled state
            if (!canProceed)
            {
                proceedToBattleButton.AddToClassList("disabled-button");
                proceedToBattleButton.tooltip = "Create at least one character to proceed";
            }
            else
            {
                proceedToBattleButton.RemoveFromClassList("disabled-button");
                proceedToBattleButton.tooltip = "Start adventure with your team";

                // Force remove any disabled classes that might be interfering
                proceedToBattleButton.RemoveFromClassList("unity-disabled");
            }


        }
        else
        {
            Debug.LogWarning("UpdateNavigationButtons: proceedToBattleButton is null! The button name in the UI might be different than expected.");
            Debug.LogWarning("Please check that a button with name 'ProceedToBattleButton' exists in your UI Document.");

            // Try to find the button again with common alternative names
            var root = uiDocument?.rootVisualElement;
            if (root != null)
            {
                proceedToBattleButton = root.Q<Button>("ProceedButton") ??
                                     root.Q<Button>("StartButton") ??
                                     root.Q<Button>("BattleButton") ??
                                     root.Q<Button>("ProceedToBattleButton");

                if (proceedToBattleButton != null)
                {

                    // Recursively call this method now that we found the button
                    UpdateNavigationButtons();
                }
            }
        }
    }

    private bool HasAtLeastOneCharacter()
    {

        int characterCount = 0;
        for (int i = 0; i < characterSlots.Length; i++)
        {
            if (characterSlots[i] != null)
            {
                characterCount++;

            }
            else
            {

            }
        }

        bool hasCharacters = characterCount > 0;


        return hasCharacters;
    }

    #endregion

    #region Save/Load Team Data

    private void LoadSavedTeamData()
    {
        // Try to load from GameStateManager first (if available and has data)
        if (GameStateManager.Instance != null && GameStateManager.Instance.savedTeam != null)
        {

            for (int i = 0; i < characterSlots.Length && i < GameStateManager.Instance.savedTeam.Length; i++)
            {
                if (GameStateManager.Instance.savedTeam[i] != null)
                {
                    characterSlots[i] = GameStateManager.Instance.savedTeam[i].CreateCopy();

                    // Update the slot button
                    if (i < characterSlotButtons.Count)
                    {
                        characterSlotButtons[i].text = characterSlots[i].name;
                        characterSlotButtons[i].SetEnabled(true);
                        characterSlotButtons[i].RemoveFromClassList("unity-disabled");
                        characterSlotButtons[i].RemoveFromClassList("DisabledCharacterCard");

                        // Hide the create button
                        if (i < createNewCharacterButtons.Count)
                        {
                            createNewCharacterButtons[i].style.display = DisplayStyle.None;
                        }
                    }
                }
            }
        }

        // Always also try to load from PlayerPrefs (either as backup or primary)

        for (int i = 0; i < characterSlots.Length; i++)
        {
            // If we haven't loaded this slot from GameStateManager, try PlayerPrefs
            if (characterSlots[i] == null && PlayerPrefs.HasKey($"Character{i}_Exists") && PlayerPrefs.GetInt($"Character{i}_Exists") == 1)
            {
                // Load character data from PlayerPrefs
                characterSlots[i] = LoadCharacterFromPlayerPrefs(i);


                // Update the slot button
                if (i < characterSlotButtons.Count)
                {
                    characterSlotButtons[i].text = characterSlots[i].name;
                    characterSlotButtons[i].SetEnabled(true);
                    characterSlotButtons[i].RemoveFromClassList("unity-disabled");
                    characterSlotButtons[i].RemoveFromClassList("DisabledCharacterCard");

                    // Hide the create button
                    if (i < createNewCharacterButtons.Count)
                    {
                        createNewCharacterButtons[i].style.display = DisplayStyle.None;
                    }
                }
            }
        }

        // Count loaded characters
        int loadedCount = 0;
        for (int i = 0; i < characterSlots.Length; i++)
        {
            if (characterSlots[i] != null) loadedCount++;
        }
    }

    private void SaveCurrentTeamToGameState()
    {
        // Save the current team using PlayerPrefs for now
        for (int i = 0; i < characterSlots.Length; i++)
        {
            if (characterSlots[i] != null)
            {
                SaveCharacterToPlayerPrefs(characterSlots[i], i);
                PlayerPrefs.SetInt($"Character{i}_Exists", 1);
            }
            else
            {
                PlayerPrefs.SetInt($"Character{i}_Exists", 0);
            }
        }
        PlayerPrefs.Save();
    }

    private TeamCharacter LoadCharacterFromPlayerPrefs(int index)
    {
        string prefix = $"Character{index}_";
        var character = new TeamCharacter();

        character.name = PlayerPrefs.GetString(prefix + "Name", "");
        character.isMale = PlayerPrefs.GetInt(prefix + "IsMale", 1) == 1;
        character.strength = PlayerPrefs.GetInt(prefix + "Strength", 10);
        character.dexterity = PlayerPrefs.GetInt(prefix + "Dexterity", 10);
        character.constitution = PlayerPrefs.GetInt(prefix + "Constitution", 10);
        character.wisdom = PlayerPrefs.GetInt(prefix + "Wisdom", 10);
        character.gold = PlayerPrefs.GetInt(prefix + "Gold", 25);
        character.silver = PlayerPrefs.GetInt(prefix + "Silver", 0);
        character.copper = PlayerPrefs.GetInt(prefix + "Copper", 0);

        // Recalculate equipment bonuses from inventory after loading
        character.RecalculateEquipmentBonuses();

        return character;
    }

    private void SaveCharacterToPlayerPrefs(TeamCharacter character, int index)
    {
        string prefix = $"Character{index}_";



        PlayerPrefs.SetString(prefix + "Name", character.name);
        PlayerPrefs.SetInt(prefix + "IsMale", character.isMale ? 1 : 0);
        PlayerPrefs.SetInt(prefix + "Strength", character.strength);
        PlayerPrefs.SetInt(prefix + "Dexterity", character.dexterity);
        PlayerPrefs.SetInt(prefix + "Constitution", character.constitution);
        PlayerPrefs.SetInt(prefix + "Wisdom", character.wisdom);
        PlayerPrefs.SetInt(prefix + "Gold", character.gold);
        PlayerPrefs.SetInt(prefix + "Silver", character.silver);
        PlayerPrefs.SetInt(prefix + "Copper", character.copper);

        // Immediately verify what was saved
        string savedName = PlayerPrefs.GetString(prefix + "Name", "FAILED_TO_SAVE");
        int savedStr = PlayerPrefs.GetInt(prefix + "Strength", -1);


        if (savedName != character.name)
        {
            Debug.LogError($"  ERROR: Name save failed! Expected '{character.name}', got '{savedName}'");
        }
    }

    #endregion

    #region Team Finalization

    private bool IsTeamFinalized()
    {
        // Check if the team has been finalized (player clicked "Proceed to Battle" and went to MapScene)
        // This is indicated by TeamSetupComplete being true or HasGeneratedMap being true
        bool hasGeneratedMap = PlayerPrefs.HasKey("HasGeneratedMap") && PlayerPrefs.GetInt("HasGeneratedMap") == 1;
        bool teamSetupCompletePrefs = PlayerPrefs.HasKey("TeamSetupComplete") && PlayerPrefs.GetInt("TeamSetupComplete") == 1;
        bool teamSetupCompleteGameState = false;

        if (GameStateManager.Instance != null)
        {
            teamSetupCompleteGameState = GameStateManager.Instance.teamSetupComplete;
        }

        bool finalized = hasGeneratedMap || teamSetupCompletePrefs || teamSetupCompleteGameState;

        return finalized;
    }

    private void SetTeamFinalizedMode()
    {


        // Disable all character creation and editing
        foreach (var button in createNewCharacterButtons)
        {
            button.SetEnabled(false);
            button.style.opacity = 0.5f;
            button.tooltip = "Team has been finalized - cannot create new characters";
        }

        // Disable character editing fields
        DisableCharacterEditingFields();

        // Change the proceed button to "Return to Adventure"
        if (proceedToBattleButton != null)
        {
            proceedToBattleButton.text = "Return to Adventure";
            proceedToBattleButton.tooltip = "Return to your ongoing adventure";
        }

        // Change the back button to offer new game option
        if (backToTitleButton != null)
        {
            backToTitleButton.text = "New Game";
            backToTitleButton.tooltip = "Start a completely new game (will overwrite current save)";
        }

        // Show a message that team is finalized

    }

    private void DisableCharacterEditingFields()
    {
        // Disable name editing
        if (characterNameField != null)
        {
            characterNameField.SetEnabled(false);
            characterNameField.style.opacity = 0.7f;
        }

        if (randomizeNameButton != null)
        {
            randomizeNameButton.SetEnabled(false);
            randomizeNameButton.style.opacity = 0.5f;
        }

        if (genderDropdown != null)
        {
            genderDropdown.SetEnabled(false);
            genderDropdown.style.opacity = 0.7f;
        }

        // Disable stat editing
        if (strengthField != null) strengthField.SetEnabled(false);
        if (dexterityField != null) dexterityField.SetEnabled(false);
        if (constitutionField != null) constitutionField.SetEnabled(false);
        if (wisdomField != null) wisdomField.SetEnabled(false);

        // Disable attribute buttons
        if (randomizeAttributesButton != null)
        {
            randomizeAttributesButton.SetEnabled(false);
            randomizeAttributesButton.style.opacity = 0.5f;
        }

        if (resetToPointBuyButton != null)
        {
            resetToPointBuyButton.SetEnabled(false);
            resetToPointBuyButton.style.opacity = 0.5f;
        }

        // Disable inventory management
        if (addWeaponButton != null)
        {
            addWeaponButton.SetEnabled(false);
            addWeaponButton.style.opacity = 0.5f;
        }

        if (addArmorButton != null)
        {
            addArmorButton.SetEnabled(false);
            addArmorButton.style.opacity = 0.5f;
        }

        if (addMiscButton != null)
        {
            addMiscButton.SetEnabled(false);
            addMiscButton.style.opacity = 0.5f;
        }

        // Disable bank editing
        if (goldField != null) goldField.SetEnabled(false);
        if (silverField != null) silverField.SetEnabled(false);
        if (copperField != null) copperField.SetEnabled(false);
    }

    #endregion

    private System.Collections.IEnumerator RefreshShopManagerReference()
    {
        // Wait for a short delay to allow for potential late initialization
        yield return new WaitForSeconds(0.1f);

        // Try to find the shop manager again
        var newShopManager = FindFirstObjectByType<SimpleShopManager>();
        if (newShopManager != null)
        {
            shopManager = newShopManager;
            SetupShopManagerCallbacks();

        }
        else
        {
            Debug.LogWarning("Still no SimpleShopManager found after delay. You may need to create one manually using the ShopSystemSetup component.");
        }
    }

    private void SetupShopManagerCallbacks()
    {
        if (shopManager != null)
        {
            // Subscribe to character data changes from the shop
            shopManager.OnCharacterDataChanged += OnShopCharacterDataChanged;
        }
    }

    private void OnShopCharacterDataChanged(TeamCharacter updatedCharacter)
    {
        if (updatedCharacter == null || currentCharacter == null)
        {
            Debug.LogWarning("OnShopCharacterDataChanged: updatedCharacter or currentCharacter is null");
            return;
        }

        // Copy the updated data back to the current character
        // This ensures the character object reference remains the same but data is updated
        currentCharacter.gold = updatedCharacter.gold;
        currentCharacter.silver = updatedCharacter.silver;
        currentCharacter.copper = updatedCharacter.copper;

        // Update inventory lists (create new lists to avoid reference issues)
        currentCharacter.weapons = new System.Collections.Generic.List<string>(updatedCharacter.weapons);
        currentCharacter.armor = new System.Collections.Generic.List<string>(updatedCharacter.armor);
        currentCharacter.miscItems = new System.Collections.Generic.List<string>(updatedCharacter.miscItems);

        // Update the UI to reflect the changes
        UpdateInventoryUI();

        // Save the updated character to the slot
        SaveCurrentCharacterToSlot();
    }

    // Clear all equipment from existing characters to ensure shop-only acquisition
    private void ClearAllCharacterEquipment()
    {
        // Clear equipment from any existing characters in slots
        for (int i = 0; i < characterSlots.Length; i++)
        {
            if (characterSlots[i] != null)
            {
                characterSlots[i].weapons.Clear();
                characterSlots[i].armor.Clear();
                characterSlots[i].miscItems.Clear();
                characterSlots[i].equippedWeapon = "";
                characterSlots[i].equippedArmor = "";
            }
        }

        // Also clear equipment from current character if it exists
        if (currentCharacter != null)
        {
            currentCharacter.weapons.Clear();
            currentCharacter.armor.Clear();
            currentCharacter.miscItems.Clear();
            currentCharacter.equippedWeapon = "";
            currentCharacter.equippedArmor = "";
        }
    }
}

[System.Serializable]
public class TeamCharacter
{
    public string name = "";
    public bool isMale = true;

    // Base attributes (can be modified by equipment in the future)
    public int strength = 10;
    public int dexterity = 10;
    public int constitution = 10;
    public int wisdom = 10;

    // Character creation mode
    public bool isRandomized = false; // Track if stats were randomized
    public int availablePoints = 18; // Point-buy system (adjusted for 4 stats instead of 6)

    // Equipment modifiers (for future use)
    public int strengthModifier = 0;
    public int dexterityModifier = 0;
    public int constitutionModifier = 0;
    public int wisdomModifier = 0;
    public int acModifier = 0; // Direct AC bonuses from equipment

    // Currency
    public int gold = 25; // Reduced starting money to make shop purchases more strategic
    public int silver = 0;
    public int copper = 0;

    // Equipment/Inventory (simple string-based for now)
    public System.Collections.Generic.List<string> weapons = new System.Collections.Generic.List<string>();
    public System.Collections.Generic.List<string> armor = new System.Collections.Generic.List<string>();
    public System.Collections.Generic.List<string> miscItems = new System.Collections.Generic.List<string>();
    public string equippedWeapon = "";
    public string equippedArmor = "";

    // Final attributes (base + equipment modifiers)
    public int FinalStrength => strength + strengthModifier;
    public int FinalDexterity => dexterity + dexterityModifier;
    public int FinalConstitution => constitution + constitutionModifier;
    public int FinalWisdom => wisdom + wisdomModifier;

    // Derived stats (calculated from final stats)
    public int Initiative => FinalDexterity - 10;
    public int DamageBonus => (FinalStrength - 10) / 2;
    public int SpellAC => FinalWisdom - 10; // Renamed from SpellCastingBonus
    public int MovementSpeed => 30 + (int)(Mathf.Ceil((FinalDexterity - 10) / 5.0f) * 5); // Rounded to closest 5 above
    public int HitPoints => Mathf.Max(10, 10 + (FinalConstitution - 10) * 2 + (FinalStrength - 10) + (FinalDexterity - 10) / 2);
    public int ArmorClass => 10 + (FinalDexterity - 10) / 2 + (FinalConstitution - 10) / 3 + acModifier; // Base 10 + DEX bonus + small CON bonus + equipment AC

    public TeamCharacter()
    {
        // Don't generate random name in constructor to avoid serialization issues
        // Name will be generated explicitly when needed
    }

    public TeamCharacter(string characterName, bool male = true)
    {
        name = characterName;
        isMale = male;
    }

    public void GenerateRandomName()
    {
        // Only generate names when the application is playing to avoid serialization issues
        if (Application.isPlaying)
        {
            name = FantasyNameGenerator.GenerateCharacterName(isMale);
        }
        else
        {
            // Fallback name for serialization/design time
            name = isMale ? "New Male Character" : "New Female Character";
        }
    }

    public void SetGender(bool male)
    {
        isMale = male;
    }

    // Randomize all attributes using point-buy system (18 points)
    public void RandomizeAttributes()
    {
        if (Application.isPlaying)
        {
            // Start with random base values (4-12 range for variety)
            int[] stats = new int[4];
            for (int i = 0; i < 4; i++)
            {
                stats[i] = UnityEngine.Random.Range(4, 13); // Random start between 4-12
            }

            // Calculate how many points we've spent so far
            int pointsSpent = 0;
            for (int i = 0; i < 4; i++)
            {
                pointsSpent += GetStatCost(stats[i]);
            }

            int pointsRemaining = 18 - pointsSpent; // Changed from 27 to 18

            // If we're over budget, reduce some stats
            while (pointsRemaining < 0)
            {
                int randomStat = UnityEngine.Random.Range(0, 4);
                if (stats[randomStat] > 4) // Don't go below 4
                {
                    int oldCost = GetStatCost(stats[randomStat]);
                    stats[randomStat]--;
                    int newCost = GetStatCost(stats[randomStat]);
                    pointsRemaining += (oldCost - newCost);
                }
            }

            // Now spend remaining points randomly
            while (pointsRemaining > 0)
            {
                // Pick a random stat to increase
                int randomStat = UnityEngine.Random.Range(0, 4);

                // Check if we can afford to increase this stat
                int currentValue = stats[randomStat];
                if (currentValue >= 18) continue; // Can't go higher than 18

                int currentCost = GetStatCost(currentValue);
                int newCost = GetStatCost(currentValue + 1);
                int costDifference = newCost - currentCost;

                if (pointsRemaining >= costDifference)
                {
                    stats[randomStat]++;
                    pointsRemaining -= costDifference;
                }
                else
                {
                    // If we can't afford any increases, try to find a stat we can afford
                    bool foundAffordable = false;
                    for (int i = 0; i < 4; i++)
                    {
                        if (stats[i] < 18)
                        {
                            int testCurrentCost = GetStatCost(stats[i]);
                            int testNewCost = GetStatCost(stats[i] + 1);
                            int testCostDiff = testNewCost - testCurrentCost;

                            if (pointsRemaining >= testCostDiff)
                            {
                                foundAffordable = true;
                                break;
                            }
                        }
                    }

                    // If no affordable increases, break out
                    if (!foundAffordable) break;
                }
            }

            // Assign back to properties
            strength = stats[0];
            dexterity = stats[1];
            constitution = stats[2];
            wisdom = stats[3];

            // Set as point-buy mode (not locked like true random)
            isRandomized = false;
            UpdateAvailablePoints();
        }
    }    // Alternative: Truly random stats (2-18, locked from editing)
    public void RandomizeAttributesWild()
    {
        if (Application.isPlaying)
        {
            strength = UnityEngine.Random.Range(2, 19); // 2-18 inclusive
            dexterity = UnityEngine.Random.Range(2, 19);
            constitution = UnityEngine.Random.Range(2, 19);
            wisdom = UnityEngine.Random.Range(2, 19);
            isRandomized = true;
            availablePoints = 0; // No points available when randomized
        }
    }

    // Reset attributes to default for point-buy
    public void ResetToPointBuy()
    {
        strength = 10;
        dexterity = 10;
        constitution = 10;
        wisdom = 10;
        isRandomized = false;
        availablePoints = 18; // Reset to full point pool (adjusted for 4 stats)
    }

    // Calculate point cost for a stat value (point-buy system)
    public static int GetStatCost(int statValue)
    {
        // Point-buy costs: 4=-4pts, 5=-2pts, 6=-1pts, 7=-1pts, 8=0pts, 9=1pt, 10=2pts, 11=3pts, 12=4pts, 13=5pts, 14=7pts, 15=9pts, 16=12pts, 17=16pts, 18=21pts
        switch (statValue)
        {
            case 4: return -4;
            case 5: return -2;
            case 6: return -1;
            case 7: return -1;
            case 8: return 0;
            case 9: return 1;
            case 10: return 2;
            case 11: return 3;
            case 12: return 4;
            case 13: return 5;
            case 14: return 7;
            case 15: return 9;
            case 16: return 12;
            case 17: return 16;
            case 18: return 21;
            default: return statValue < 4 ? -4 : 25; // Invalid values
        }
    }

    // Calculate total points spent
    public int GetTotalPointsSpent()
    {
        return GetStatCost(strength) + GetStatCost(dexterity) + GetStatCost(constitution) + GetStatCost(wisdom);
    }

    // Update available points based on current stats
    public void UpdateAvailablePoints()
    {
        if (!isRandomized)
        {
            availablePoints = 18 - GetTotalPointsSpent(); // Changed from 27 to 18
        }
    }

    // Check if we can afford to increase a stat
    public bool CanIncreaseStat(int currentValue)
    {
        if (isRandomized) return false; // Can't change randomized stats
        if (currentValue >= 18) return false; // Max value

        int currentCost = GetStatCost(currentValue);
        int newCost = GetStatCost(currentValue + 1);
        int costDifference = newCost - currentCost;

        return availablePoints >= costDifference;
    }

    // Create a deep copy of this character
    public TeamCharacter CreateCopy()
    {
        var copy = new TeamCharacter(name, isMale)
        {
            strength = this.strength,
            dexterity = this.dexterity,
            constitution = this.constitution,
            wisdom = this.wisdom,
            isRandomized = this.isRandomized,
            availablePoints = this.availablePoints,
            gold = this.gold,
            silver = this.silver,
            copper = this.copper,
            equippedWeapon = this.equippedWeapon,
            equippedArmor = this.equippedArmor
        };

        // Deep copy the lists
        copy.weapons = new System.Collections.Generic.List<string>(this.weapons);
        copy.armor = new System.Collections.Generic.List<string>(this.armor);
        copy.miscItems = new System.Collections.Generic.List<string>(this.miscItems);

        return copy;
    }

    // Recalculate all equipment modifiers from items in inventory (temporary solution)
    public void RecalculateEquipmentBonuses()
    {
        // Reset all modifiers to zero
        strengthModifier = 0;
        dexterityModifier = 0;
        constitutionModifier = 0;
        wisdomModifier = 0;
        acModifier = 0;

        // Get reference to the shop manager to access item definitions
        var shopManager = UnityEngine.Object.FindFirstObjectByType<SimpleShopManager>();
        if (shopManager == null) return;

        // Apply bonuses from all items in inventory
        ApplyBonusesFromItemList(shopManager.weapons, weapons);
        ApplyBonusesFromItemList(shopManager.armor, armor);
        ApplyBonusesFromItemList(shopManager.miscItems, miscItems);
    }

    private void ApplyBonusesFromItemList(System.Collections.Generic.List<SimpleShopItem> shopItems, System.Collections.Generic.List<string> ownedItems)
    {
        foreach (string itemName in ownedItems)
        {
            var shopItem = shopItems.Find(item => item.name == itemName);
            if (shopItem != null && shopItem.stats != null)
            {
                strengthModifier += shopItem.stats.strengthBonus;
                dexterityModifier += shopItem.stats.dexterityBonus;
                constitutionModifier += shopItem.stats.constitutionBonus;
                wisdomModifier += shopItem.stats.wisdomBonus;
                acModifier += shopItem.stats.acBonus;
            }
        }
    }

    // Copy data from another character into this one
    public void CopyFrom(TeamCharacter other)
    {
        if (other == null) return;

        name = other.name;
        isMale = other.isMale;
        strength = other.strength;
        dexterity = other.dexterity;
        constitution = other.constitution;
        wisdom = other.wisdom;
    }
}
