package com.tiedup.remake.v2.furniture;

import net.minecraft.world.phys.Vec3;

/**
 * Side-neutral math helpers for the server-side fallback seat geometry used
 * when GLB-baked {@code SeatTransform} data is unavailable.
 *
 * <p>Seats are placed evenly along the furniture entity's local right axis:
 * for {@code n} seats the offsets are {@code -(n-1)/2, -(n-1)/2 + 1, …, (n-1)/2}
 * (so 1 seat → center; 3 seats → -1, 0, 1). The right axis is derived from
 * the entity's yaw.</p>
 *
 * <p>Previously inlined at three {@code EntityFurniture} sites (positionRider,
 * findNearestAvailableSeat, findNearestOccupiedLockableSeat) with identical
 * formulas. Centralised here so any future tweak (e.g. per-definition seat
 * spacing) updates one place.</p>
 */
public final class FurnitureSeatGeometry {

    private FurnitureSeatGeometry() {}

    /**
     * Entity-local right axis in world XZ coordinates for the given yaw.
     * Y component is always 0.
     *
     * @param yawDegrees entity yaw in degrees (as {@code Entity.getYRot()})
     * @return unit vector pointing to the entity's right in world space
     */
    public static Vec3 rightAxis(float yawDegrees) {
        double yawRad = Math.toRadians(yawDegrees);
        double rightX = -Math.sin(yawRad + Math.PI / 2.0);
        double rightZ = Math.cos(yawRad + Math.PI / 2.0);
        return new Vec3(rightX, 0.0, rightZ);
    }

    /**
     * Local offset along the right axis for {@code seatIndex} among
     * {@code seatCount} evenly-spaced seats.
     *
     * <p>Examples: 1 seat → 0.0 (centered). 2 seats → -0.5, 0.5. 3 seats →
     * -1.0, 0.0, 1.0.</p>
     *
     * @param seatIndex zero-based seat index
     * @param seatCount total seats on the furniture (must be ≥ 1)
     * @return signed distance from centre along the right axis
     */
    public static double seatOffset(int seatIndex, int seatCount) {
        return seatCount == 1 ? 0.0 : (seatIndex - (seatCount - 1) / 2.0);
    }
}
