package com.tiedup.remake.v2.furniture;

import java.util.List;
import java.util.Map;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

/**
 * Immutable definition for a data-driven furniture piece.
 *
 * <p>Loaded from JSON files in {@code data/<namespace>/tiedup_furniture/}.
 * Synced to clients via {@code PacketSyncFurnitureDefinitions}.
 * Each definition describes placement rules, visual properties,
 * seat layout, and interaction feedback for a furniture type.</p>
 *
 * <p>All rendering and gameplay properties are read from this record at runtime
 * via the furniture entity and renderer.</p>
 */
public record FurnitureDefinition(
    /** Unique identifier (e.g., "tiedup:wooden_stocks"). */
    ResourceLocation id,

    /** Human-readable display name (fallback if no translation key). */
    String displayName,

    /** Optional translation key for localized display name. */
    @Nullable String translationKey,

    /** Resource location of the GLB model file. */
    ResourceLocation modelLocation,

    /** Tint channel defaults: channel name to ARGB color (e.g., "tintable_0" -> 0x8B4513). */
    Map<String, Integer> tintChannels,

    /** Whether this furniture supports player-applied color customization. */
    boolean supportsColor,

    /** Collision box width in blocks (X/Z axis). */
    float hitboxWidth,

    /** Collision box height in blocks (Y axis). */
    float hitboxHeight,

    /** Whether this furniture snaps to adjacent walls on placement. */
    boolean snapToWall,

    /** Whether this furniture can only be placed on solid ground. */
    boolean floorOnly,

    /** Whether this furniture can be locked with a key item. */
    boolean lockable,

    /** Resistance to breaking (higher = harder to destroy). */
    float breakResistance,

    /** Whether the furniture drops as an item when broken. */
    boolean dropOnBreak,

    /** Ordered list of seat definitions. Index is used for bitmask operations. */
    List<SeatDefinition> seats,

    /** Optional sound overrides for interactions. */
    FurnitureFeedback feedback,

    /** Grouping category for creative menu / UI filtering (e.g., "restraint", "decoration"). */
    String category,

    /**
     * Optional inventory icon model location (e.g., "tiedup:item/wooden_stocks").
     *
     * <p>Points to a standard {@code item/generated} model JSON that will be used
     * as the inventory sprite for this furniture variant when held as a placer item.
     * When null, the default {@code tiedup:item/furniture_placer} model is used.</p>
     */
    @Nullable ResourceLocation icon
) {
    /**
     * Find a seat definition by its unique ID.
     *
     * @param seatId the seat identifier to search for
     * @return the matching {@link SeatDefinition}, or null if not found
     */
    @Nullable
    public SeatDefinition getSeat(String seatId) {
        for (SeatDefinition seat : seats) {
            if (seat.id().equals(seatId)) return seat;
        }
        return null;
    }

    /**
     * Get the positional index of a seat (for bitmask operations on lock state, occupancy, etc.).
     *
     * @param seatId the seat identifier to search for
     * @return the zero-based index, or -1 if not found
     */
    public int getSeatIndex(String seatId) {
        for (int i = 0; i < seats.size(); i++) {
            if (seats.get(i).id().equals(seatId)) return i;
        }
        return -1;
    }
}
