/**
 * Default weapon types based on Prism RPG rules
 * APC determines weapon class: Light (1 APC), One-Handed (2 APC), Heavy (3 APC)
 */
const DEFAULT_TYPES = {
  light: {
    id: "light",
    label: "PRISMRPG.Weapon.Type.light",
    apc: 1,
    hands: 1
  },
  oneHanded: {
    id: "oneHanded",
    label: "PRISMRPG.Weapon.Type.oneHanded",
    apc: 2,
    hands: 1
  },
  heavy: {
    id: "heavy",
    label: "PRISMRPG.Weapon.Type.heavy",
    apc: 3,
    hands: 2
  },
  projectile: {
    id: "projectile",
    label: "PRISMRPG.Weapon.Type.projectile",
    apc: 0, // Variable based on specific weapon
    hands: 2
  }
};

/**
 * Get weapon types (default + custom from settings)
 */
export function getWeaponTypes() {
  if (!game?.settings) return DEFAULT_TYPES;

  const customTypes = game.settings.get("fvtt-prism-rpg", "customWeaponTypes") || {};
  return foundry.utils.mergeObject(DEFAULT_TYPES, customTypes, { inplace: false });
}

/**
 * Weapon types (dynamically loaded)
 */
export const TYPE = new Proxy({}, {
  get(target, prop) {
    const types = getWeaponTypes();
    return types[prop];
  },
  ownKeys(target) {
    return Object.keys(getWeaponTypes());
  },
  getOwnPropertyDescriptor(target, prop) {
    return {
      enumerable: true,
      configurable: true
    };
  }
});

/**
 * Simplified Weapon Types object for form choices (label-only format)
 */
export function getWeaponTypeChoices() {
  const types = getWeaponTypes();
  return Object.fromEntries(
    Object.entries(types).map(([key, value]) => [key, value.label])
  );
}

export const TYPE_CHOICES = new Proxy({}, {
  get(target, prop) {
    const choices = getWeaponTypeChoices();
    return choices[prop];
  },
  ownKeys(target) {
    return Object.keys(getWeaponTypeChoices());
  },
  getOwnPropertyDescriptor(target, prop) {
    return {
      enumerable: true,
      configurable: true
    };
  }
});

/**
 * Default weapon groups and their associated passives
 * Each weapon belongs to a group and possesses its passive while wielded
 */
const DEFAULT_WEAPON_GROUPS = {
  shortsword: {
    id: "shortsword",
    label: "PRISMRPG.WeaponGroup.shortsword",
    passive: "quickBlade",
    passiveLabel: "PRISMRPG.Weapon.Passive.quickBlade",
    passiveDescription: "PRISMRPG.Weapon.PassiveDescription.quickBlade"
  },
  longsword: {
    id: "longsword",
    label: "PRISMRPG.WeaponGroup.longsword",
    passive: "turningEdge",
    passiveLabel: "PRISMRPG.Weapon.Passive.turningEdge",
    passiveDescription: "PRISMRPG.Weapon.PassiveDescription.turningEdge"
  },
  greatsword: {
    id: "greatsword",
    label: "PRISMRPG.WeaponGroup.greatsword",
    passive: "cleave",
    passiveLabel: "PRISMRPG.Weapon.Passive.cleave",
    passiveDescription: "PRISMRPG.Weapon.PassiveDescription.cleave"
  },
  handaxe: {
    id: "handaxe",
    label: "PRISMRPG.WeaponGroup.handaxe",
    passive: "throwingAxe",
    passiveLabel: "PRISMRPG.Weapon.Passive.throwingAxe",
    passiveDescription: "PRISMRPG.Weapon.PassiveDescription.throwingAxe"
  },
  battleaxe: {
    id: "battleaxe",
    label: "PRISMRPG.WeaponGroup.battleaxe",
    passive: "shieldEater",
    passiveLabel: "PRISMRPG.Weapon.Passive.shieldEater",
    passiveDescription: "PRISMRPG.Weapon.PassiveDescription.shieldEater"
  },
  greataxe: {
    id: "greataxe",
    label: "PRISMRPG.WeaponGroup.greataxe",
    passive: "devastatingBlow",
    passiveLabel: "PRISMRPG.Weapon.Passive.devastatingBlow",
    passiveDescription: "PRISMRPG.Weapon.PassiveDescription.devastatingBlow"
  },
  club: {
    id: "club",
    label: "PRISMRPG.WeaponGroup.club",
    passive: "stun",
    passiveLabel: "PRISMRPG.Weapon.Passive.stun",
    passiveDescription: "PRISMRPG.Weapon.PassiveDescription.stun"
  },
  mace: {
    id: "mace",
    label: "PRISMRPG.WeaponGroup.mace",
    passive: "armorBreaker",
    passiveLabel: "PRISMRPG.Weapon.Passive.armorBreaker",
    passiveDescription: "PRISMRPG.Weapon.PassiveDescription.armorBreaker"
  },
  greatMaul: {
    id: "greatMaul",
    label: "PRISMRPG.WeaponGroup.greatMaul",
    passive: "earthshatter",
    passiveLabel: "PRISMRPG.Weapon.Passive.earthshatter",
    passiveDescription: "PRISMRPG.Weapon.PassiveDescription.earthshatter"
  },
  javelin: {
    id: "javelin",
    label: "PRISMRPG.WeaponGroup.javelin",
    passive: "piercingThrow",
    passiveLabel: "PRISMRPG.Weapon.Passive.piercingThrow",
    passiveDescription: "PRISMRPG.Weapon.PassiveDescription.piercingThrow"
  },
  spear: {
    id: "spear",
    label: "PRISMRPG.WeaponGroup.spear",
    passive: "reach",
    passiveLabel: "PRISMRPG.Weapon.Passive.reach",
    passiveDescription: "PRISMRPG.Weapon.PassiveDescription.reach"
  },
  longSpear: {
    id: "longSpear",
    label: "PRISMRPG.WeaponGroup.longSpear",
    passive: "extendedReach",
    passiveLabel: "PRISMRPG.Weapon.Passive.extendedReach",
    passiveDescription: "PRISMRPG.Weapon.PassiveDescription.extendedReach"
  },
  warhammer: {
    id: "warhammer",
    label: "PRISMRPG.WeaponGroup.warhammer",
    passive: "puncturingBlows",
    passiveLabel: "PRISMRPG.Weapon.Passive.puncturingBlows",
    passiveDescription: "PRISMRPG.Weapon.PassiveDescription.puncturingBlows"
  },
  dagger: {
    id: "dagger",
    label: "PRISMRPG.WeaponGroup.dagger",
    passive: "balancingStance",
    passiveLabel: "PRISMRPG.Weapon.Passive.balancingStance",
    passiveDescription: "PRISMRPG.Weapon.PassiveDescription.balancingStance"
  },
  crossbow: {
    id: "crossbow",
    label: "PRISMRPG.WeaponGroup.crossbow",
    passive: "boltlock",
    passiveLabel: "PRISMRPG.Weapon.Passive.boltlock",
    passiveDescription: "PRISMRPG.Weapon.PassiveDescription.boltlock"
  },
  longbow: {
    id: "longbow",
    label: "PRISMRPG.WeaponGroup.longbow",
    passive: "volleyFire",
    passiveLabel: "PRISMRPG.Weapon.Passive.volleyFire",
    passiveDescription: "PRISMRPG.Weapon.PassiveDescription.volleyFire"
  }
};

/**
 * Get weapon groups (default + custom from settings)
 */
export function getWeaponGroups() {
  if (!game?.settings) return DEFAULT_WEAPON_GROUPS;

  const customGroups = game.settings.get("fvtt-prism-rpg", "customWeaponGroups") || {};
  return foundry.utils.mergeObject(DEFAULT_WEAPON_GROUPS, customGroups, { inplace: false });
}

/**
 * Weapon groups (dynamically loaded)
 */
export const WEAPON_GROUP = new Proxy({}, {
  get(target, prop) {
    const groups = getWeaponGroups();
    return groups[prop];
  },
  ownKeys(target) {
    return Object.keys(getWeaponGroups());
  },
  getOwnPropertyDescriptor(target, prop) {
    return {
      enumerable: true,
      configurable: true
    };
  }
});

/**
 * Simplified Weapon Groups object for form choices (label-only format)
 */
export function getWeaponGroupChoices() {
  const groups = getWeaponGroups();
  return Object.fromEntries(
    Object.entries(groups).map(([key, value]) => [key, value.label])
  );
}

export const WEAPON_GROUP_CHOICES = new Proxy({}, {
  get(target, prop) {
    const choices = getWeaponGroupChoices();
    return choices[prop];
  },
  ownKeys(target) {
    return Object.keys(getWeaponGroupChoices());
  },
  getOwnPropertyDescriptor(target, prop) {
    return {
      enumerable: true,
      configurable: true
    };
  }
});

/**
 * Damage types for weapons
 */
export const DAMAGE_TYPE = Object.freeze({
  piercing: {
    id: "piercing",
    label: "PRISMRPG.Weapon.DamageType.piercing",
    abbreviation: "P"
  },
  bludgeoning: {
    id: "bludgeoning",
    label: "PRISMRPG.Weapon.DamageType.bludgeoning",
    abbreviation: "B"
  },
  slashing: {
    id: "slashing",
    label: "PRISMRPG.Weapon.DamageType.slashing",
    abbreviation: "S"
  }
});

// Legacy exports for backward compatibility (to be removed later)
export const WEAPON_TYPE = {
  "melee": "PRISMRPG.Weapon.WeaponType.melee",
  "ranged": "PRISMRPG.Weapon.WeaponType.ranged"
}

export const WEAPON_CLASS = {
  "longblade": "PRISMRPG.Weapon.WeaponClass.longblade",
  "shortblade": "PRISMRPG.Weapon.WeaponClass.shortblade",
  "mediumblade": "PRISMRPG.Weapon.WeaponClass.mediumblade",
  "axe": "PRISMRPG.Weapon.WeaponClass.axe",
  "hammer": "PRISMRPG.Weapon.WeaponClass.hammer",
  "mace": "PRISMRPG.Weapon.WeaponClass.mace",
  "flail": "PRISMRPG.Weapon.WeaponClass.flail",
  "bow": "PRISMRPG.Weapon.WeaponClass.bow",
  "sling": "PRISMRPG.Weapon.WeaponClass.sling",
  "thrown": "PRISMRPG.Weapon.WeaponClass.thrown",
  "polearm": "PRISMRPG.Weapon.WeaponClass.polearm",
  "unarmed": "PRISMRPG.Weapon.WeaponClass.unarmed"
}

export const WEAPON_CATEGORIES = {
  "longblade": ["mediumblade", "shortblade"],
  "shortblade": ["mediumblade", "longblade"],
  "mediumblade": ["shortblade", "longblade"],
  "axe": ["hammer", "mace", "flail", "bow", "sling", "thrown", "polearm"],
  "hammer": ["axe", "mace", "flail", "bow", "sling", "thrown", "polearm"],
  "mace": ["axe", "hammer", "flail", "bow", "sling", "thrown", "polearm"],
  "flail": ["axe", "hammer", "mace", "bow", "sling", "thrown", "polearm"],
  "bow": ["axe", "hammer", "mace", "flail", "sling", "thrown", "polearm"],
  "sling": ["axe", "hammer", "mace", "flail", "bow", "thrown", "polearm"],
  "thrown": ["axe", "hammer", "mace", "flail", "bow", "sling", "polearm"],
  "polearm": ["axe", "hammer", "mace", "flail", "bow", "sling", "thrown"]
}
