/**
 * memorySummary.ts — pure helpers for the memory panel: count entries per type
 * and filter by type / free-text. Kept separate from the component so it's
 * unit-testable.
 */

export type MemoryType = 'user' | 'feedback' | 'project' | 'reference';

export const MEMORY_TYPES: MemoryType[] = ['user', 'feedback', 'project', 'reference'];

export interface MemoryEntryLike {
  name: string;
  description: string;
  type: MemoryType;
  body: string;
}

export interface MemorySummary {
  total: number;
  byType: Record<MemoryType, number>;
}

export function summarizeMemory(entries: MemoryEntryLike[]): MemorySummary {
  const byType: Record<MemoryType, number> = { user: 0, feedback: 0, project: 0, reference: 0 };
  for (const e of entries) {
    if (e.type in byType) byType[e.type] += 1;
  }
  return { total: entries.length, byType };
}

export function filterMemory(
  entries: MemoryEntryLike[],
  opts: { query?: string; type?: MemoryType | null },
): MemoryEntryLike[] {
  const q = (opts.query ?? '').trim().toLowerCase();
  return entries.filter((e) => {
    if (opts.type && e.type !== opts.type) return false;
    if (!q) return true;
    return (
      e.name.toLowerCase().includes(q) ||
      e.description.toLowerCase().includes(q) ||
      e.body.toLowerCase().includes(q)
    );
  });
}
