export const UFO_FAMILY = "pb_plane_ufo";
export const TRACTOR_DURATION_TICKS = 60;
export const TRACTOR_BEAM_RANGE = 20;

const ALLOWED_ENTITY_TYPES = [
  { id: "minecraft:item", hoverHeight: 0.2 }, // Closer range to allow it to be picked up by the player
  // Small mobs
  { id: "minecraft:allay", hoverHeight: 1.6 },
  { id: "minecraft:armadillo", hoverHeight: 1.6 },
  { id: "minecraft:axolotl", hoverHeight: 1.6 },
  { id: "minecraft:bat", hoverHeight: 1.6 },
  { id: "minecraft:bee", hoverHeight: 1.6 },
  { id: "minecraft:cat", hoverHeight: 1.6 },
  { id: "minecraft:cave_spider", hoverHeight: 1.6 },
  { id: "minecraft:chicken", hoverHeight: 1.6 },
  { id: "minecraft:dolphin", hoverHeight: 1.6 },
  { id: "minecraft:endermite", hoverHeight: 1.6 },
  { id: "minecraft:fish", hoverHeight: 1.6 },
  { id: "minecraft:fox", hoverHeight: 1.6 },
  { id: "minecraft:frog", hoverHeight: 1.6 },
  { id: "minecraft:ocelot", hoverHeight: 1.6 },
  { id: "minecraft:parrot", hoverHeight: 1.6 },
  { id: "minecraft:pufferfish", hoverHeight: 1.6 },
  { id: "minecraft:rabbit", hoverHeight: 1.6 },
  { id: "minecraft:salmon", hoverHeight: 1.6 },
  { id: "minecraft:shulker", hoverHeight: 1.6 },
  { id: "minecraft:silverfish", hoverHeight: 1.6 },
  { id: "minecraft:tadpole", hoverHeight: 1.6 },
  { id: "minecraft:tropical_fish", hoverHeight: 1.6 },
  { id: "minecraft:vex", hoverHeight: 1.6 },
  // Medium mobs
  { id: "minecraft:blaze", hoverHeight: 2.5 },
  { id: "minecraft:bogged", hoverHeight: 2.5 },
  { id: "minecraft:breeze", hoverHeight: 2.5 },
  { id: "minecraft:cow", hoverHeight: 2.5 },
  { id: "minecraft:creaking", hoverHeight: 2.5 },
  { id: "minecraft:creeper", hoverHeight: 2.5 },
  { id: "minecraft:drowned", hoverHeight: 2.5 },
  { id: "minecraft:evocation_illager", hoverHeight: 2.5 },
  { id: "minecraft:glow_squid", hoverHeight: 2.5 },
  { id: "minecraft:goat", hoverHeight: 2.5 },
  { id: "minecraft:guardian", hoverHeight: 2.5 },
  { id: "minecraft:husk", hoverHeight: 2.5 },
  { id: "minecraft:mooshroom", hoverHeight: 2.5 },
  { id: "minecraft:panda", hoverHeight: 2.5 },
  { id: "minecraft:phantom", hoverHeight: 2.5 },
  { id: "minecraft:pig", hoverHeight: 2.5 },
  { id: "minecraft:piglin_brute", hoverHeight: 2.5 },
  { id: "minecraft:piglin", hoverHeight: 2.5 },
  { id: "minecraft:pillager", hoverHeight: 2.5 },
  { id: "minecraft:polar_bear", hoverHeight: 2.5 },
  { id: "minecraft:sheep", hoverHeight: 2.5 },
  { id: "minecraft:skeleton", hoverHeight: 2.5 },
  { id: "minecraft:snow_golem", hoverHeight: 2.5 },
  { id: "minecraft:spider", hoverHeight: 2.5 },
  { id: "minecraft:squid", hoverHeight: 2.5 },
  { id: "minecraft:stray", hoverHeight: 2.5 },
  { id: "minecraft:strider", hoverHeight: 2.5 },
  { id: "minecraft:turtle", hoverHeight: 2.5 },
  { id: "minecraft:villager_v2", hoverHeight: 2.5 },
  { id: "minecraft:vindicator", hoverHeight: 2.5 },
  { id: "minecraft:wandering_trader", hoverHeight: 2.5 },
  { id: "minecraft:witch", hoverHeight: 2.5 },
  { id: "minecraft:wolf", hoverHeight: 2.5 },
  { id: "minecraft:zombie_pigman", hoverHeight: 2.5 },
  { id: "minecraft:zombie_villager_v2", hoverHeight: 2.5 },
  { id: "minecraft:zombie", hoverHeight: 2.5 },
  // Large mobs
  { id: "minecraft:camel", hoverHeight: 3 },
  { id: "minecraft:donkey", hoverHeight: 3 },
  { id: "minecraft:enderman", hoverHeight: 3 },
  { id: "minecraft:hoglin", hoverHeight: 3 },
  { id: "minecraft:horse", hoverHeight: 3 },
  { id: "minecraft:llama", hoverHeight: 3 },
  { id: "minecraft:magma_cube", hoverHeight: 3 },
  { id: "minecraft:mule", hoverHeight: 3 },
  { id: "minecraft:skeleton_horse", hoverHeight: 3 },
  { id: "minecraft:slime", hoverHeight: 3 },
  { id: "minecraft:trader_llama", hoverHeight: 3 },
  { id: "minecraft:wither_skeleton", hoverHeight: 3 },
  { id: "minecraft:zoglin", hoverHeight: 3 },
  { id: "minecraft:zombie_horse", hoverHeight: 3 },
  // Extra Large mobs
  { id: "minecraft:iron_golem", hoverHeight: 3.5 },
  { id: "minecraft:ravager", hoverHeight: 3.5 },
  { id: "minecraft:sniffer", hoverHeight: 3.5 },
  { id: "minecraft:warden", hoverHeight: 3.5 },
  // Non Vanilla entities
  { id: "pb_plane:item_crate", hoverHeight: 2.2 },
];

function isAllowedEntityType(typeId) {
  return ALLOWED_ENTITY_TYPES.some(type => type.id === typeId);
}

export function getHoverHeightForEntity(entity) {
  const entry = ALLOWED_ENTITY_TYPES.find(type => type.id === entity.typeId);
  return entry?.hoverHeight ?? 2.5; // default if not found
}

export function findEntityBelow(ufo) {
  const dim = ufo.dimension;
  const origin = ufo.location;

  const verticalRange = TRACTOR_BEAM_RANGE; // ⬇️ Increase vertical scan range
  const horizontalRadius = 3; // ⬅️➡️ Increase detection radius

  for (let yOffset = 1; yOffset <= verticalRange; yOffset++) {
    const checkPos = {
      x: origin.x,
      y: origin.y - yOffset,
      z: origin.z,
    };

    const candidates = dim.getEntities({
      location: checkPos,
      maxDistance: horizontalRadius,
      excludeFamilies: [UFO_FAMILY],
      excludeTags: ["pb_carried_by_ufo"],
    });

    for (const entity of candidates) {
      if (isAllowedEntityType(entity.typeId)) {
        return entity;
      }
    }
  }

  return undefined;
}

export function carryEntity(ufo, target) {
  target.addTag("pb_carried_by_ufo");
  target.setDynamicProperty("carried_by", ufo.id);
  target.setDynamicProperty("lift_progress", 0);
  target.setDynamicProperty("carried_ticks", 0);

  if (target.setVelocity) {
    target.setVelocity({ x: 0, y: 0, z: 0 });
  }

  //console.warn(`Entity ${target.typeId} now carried by UFO ${ufo.id}`);
}

export function getCarriedEntity(ufo) {
  const dim = ufo.dimension;
  const nearby = dim.getEntities({
    location: ufo.location,
    maxDistance: TRACTOR_BEAM_RANGE + 2, // increased from 8
    tags: ["pb_carried_by_ufo"],
  });

  return nearby.find((e) => e.getDynamicProperty("carried_by") === ufo.id);
}

export function releaseCarriedEntity(ufo) {
  ufo.setProperty("pb_plane:tractor_carrying", false);
  ufo.setProperty("pb_plane:tractor_active", false);
  const carried = getCarriedEntity(ufo);
  if (!carried) return;

  carried.removeTag("pb_carried_by_ufo");
  carried.setDynamicProperty("carried_by", undefined);
  carried.setDynamicProperty("carried_ticks", undefined);

  carried.applyImpulse({ x: 0, y: -0.2, z: 0 });
  //console.warn(`Entity released from UFO ${ufo.id}`);
}

export function tooFarFromUfo(ufo, entity) {
  const maxDistance = TRACTOR_BEAM_RANGE + 2;
  const dx = ufo.location.x - entity.location.x;
  const dy = ufo.location.y - entity.location.y;
  const dz = ufo.location.z - entity.location.z;
  const distSquared = dx * dx + dy * dy + dz * dz;
  return distSquared > maxDistance * maxDistance;
}
