import type { InventoryState } from "./inventory.js";
import type { Vec3 } from "./math.js";

/**
 * Save payload — serialised straight into the `saves.data` JSONB column
 * (GDD §9). Versioned so migrations stay possible once campaigns ship.
 */
export const SAVE_FORMAT_VERSION = 1;

export interface PlayerSaveState {
  roomId: string;
  position: Vec3;
  yaw: number;
  health: number;
  inventory: InventoryState;
}

export interface SaveData {
  version: number;
  campaign: string;
  player: PlayerSaveState;
  /** Puzzle/door/interaction flags. Presence in the set means "set". */
  flags: string[];
  /** Interactables already consumed (non-repeatable ones that fired). */
  consumedInteractables: string[];
  playtimeSeconds: number;
  savedAt: string;
}

export function emptySave(campaign: string, spawnRoomId: string): SaveData {
  return {
    version: SAVE_FORMAT_VERSION,
    campaign,
    player: {
      roomId: spawnRoomId,
      position: { x: 0, y: 0, z: 0 },
      yaw: 0,
      health: 100,
      inventory: { slots: Array(8).fill(null), capacity: 8, keyItems: [] },
    },
    flags: [],
    consumedInteractables: [],
    playtimeSeconds: 0,
    savedAt: new Date().toISOString(),
  };
}
