// lib/process/case-artifacts-query.js — fetch artifact rows by id list.
//
// node:sqlite returns null-prototype rows; React Server Components reject
// those when handing props to a Client Component. Spread into plain objects.

import { getDb } from '../db.js';

export function listArtifactsByIds(ids) {
  if (!Array.isArray(ids) || ids.length === 0) return [];
  const placeholders = ids.map(() => '?').join(',');
  return getDb()
    .prepare(`SELECT id, title, kind, path, content, created_at FROM artifacts WHERE id IN (${placeholders}) ORDER BY created_at ASC`)
    .all(...ids)
    .map((row) => ({
      id: row.id,
      title: row.title,
      kind: row.kind,
      path: row.path,
      content: row.content,
      created_at: row.created_at,
    }));
}
