package thb.jeanluc.adventure.save;

import java.util.List;
import java.util.Map;

/**
 * JSON-serialised snapshot of the mutable game state — a delta laid over the
 * world freshly loaded from YAML. Header fields ({@code slotName}, room,
 * {@code turn}, {@code savedAtEpochMillis}) double as the slot-list metadata.
 */
public record SaveData(
        int schemaVersion,
        String worldTitle,
        String slotName,
        long savedAtEpochMillis,
        int turn,
        String currentRoomId,
        List<String> visitedRoomIds,
        int gold,
        List<String> inventoryItemIds,
        List<String> flags,
        Map<String, Integer> questStages,
        List<String> questCompleted,
        Map<String, List<String>> roomItemIds,
        Map<String, Boolean> switchStates
) {
    /** Current on-disk schema version. */
    public static final int CURRENT_VERSION = 1;

    /**
     * Compact canonical constructor: normalizes any null collection component
     * to an empty immutable collection so a truncated/hand-edited save JSON
     * degrades gracefully instead of NPE-ing in {@link SaveCodec#apply}.
     */
    public SaveData {
        if (visitedRoomIds == null)   visitedRoomIds   = List.of();
        if (inventoryItemIds == null) inventoryItemIds = List.of();
        if (flags == null)            flags            = List.of();
        if (questStages == null)      questStages      = Map.of();
        if (questCompleted == null)   questCompleted   = List.of();
        if (roomItemIds == null)      roomItemIds      = Map.of();
        if (switchStates == null)     switchStates     = Map.of();
    }
}
