/**
 * Returns the score of a skill for an actor.
 * Skills are stored as a flat object in actor.system.skills: { [key]: { value, spent, label, category } }
 * @param {VermineActor} actor
 * @param {string} skillLabel - The localized label of the skill to find
 * @param {"value"|"spent"} [property="value"] - Which property to return
 * @returns {number|null} The skill score or null if not found
 */
export function getActorSkillScore(actor, skillLabel, property = "value") {
  const skills = actor.system?.skills;
  if (!skills) return null;

  for (const key of Object.keys(skills)) {
    if (skills[key].label === skillLabel) {
      return skills[key][property] ?? null;
    }
  }
  return null;
}

/**
 * Updates the score of a skill for an actor.
 * @param {VermineActor} selectedActor
 * @param {string} skillLabel - The localized label of the skill to update
 * @param {"value"|"spent"} [property="value"] - Which property to update
 * @param {number} updatedValue - The new value
 * @returns {boolean} Whether the update was successful
 */
export function updateActorSkillScore(selectedActor, skillLabel, property = "value", updatedValue) {
  try {
    const skills = selectedActor.system?.skills;
    if (!skills) return false;

    for (const key of Object.keys(skills)) {
      if (skills[key].label === skillLabel) {
        skills[key][property] = updatedValue;
        selectedActor.update({ [`system.skills.${key}.${property}`]: updatedValue });
        return true;
      }
    }
    return false;
  } catch (e) {
    return false;
  }
}
