import type { AdventureLogEntry } from './types';
import type { LogEntry } from '../network/api';

/** Map GET /hero/log lines to UI entries (oldest first, stable ids from DB). */
export function adventureEntriesFromServerLog(serverLog: LogEntry[]): {
  entries: AdventureLogEntry[];
  maxId: number;
} {
  const sorted = [...serverLog].sort(
    (a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime(),
  );
  const entries: AdventureLogEntry[] = sorted.map((entry) => ({
    id: Number(entry.id),
    message: entry.message,
    timestamp: new Date(entry.createdAt).getTime(),
  }));
  const maxId = entries.reduce((m, e) => Math.max(m, e.id), 0);
  return { entries, maxId };
}
