package actions

import (
	"dk/internal/models/items"
	"dk/internal/models/users"
)

// UserEquipItem calculates equipment updates for a user equipping an item.
// Returns map of database field updates without modifying the user struct.
func UserEquipItem(user *users.User, item *items.Item) map[string]any {
	updates := make(map[string]any)

	// Calculate stat changes
	newAttack := user.Attack
	newDefense := user.Defense

	// Remove old item stats if slot occupied
	switch item.Type {
	case items.TypeWeapon:
		if user.WeaponID != 0 {
			if oldItem, err := items.Find(user.WeaponID); err == nil {
				newAttack -= oldItem.Att
			}
		}
		// Add new item
		newAttack += item.Att
		updates["weapon_id"] = item.ID
		updates["weapon_name"] = item.Name

	case items.TypeArmor:
		if user.ArmorID != 0 {
			if oldItem, err := items.Find(user.ArmorID); err == nil {
				newDefense -= oldItem.Att
			}
		}
		// Add new item
		newDefense += item.Att
		updates["armor_id"] = item.ID
		updates["armor_name"] = item.Name

	case items.TypeShield:
		if user.ShieldID != 0 {
			if oldItem, err := items.Find(user.ShieldID); err == nil {
				newDefense -= oldItem.Att
			}
		}
		// Add new item
		newDefense += item.Att
		updates["shield_id"] = item.ID
		updates["shield_name"] = item.Name
	}

	updates["attack"] = newAttack
	updates["defense"] = newDefense

	return updates
}
