import { sendJson, serverTimestamp } from '../http.js';
import { getCategoryList } from '../queries/category-list.js';

export const method = 'GET';
export const path = '/v1/category';

export async function handle(_req, res, { url }) {
  const cursor = Number(url.searchParams.get('lastChangedCategory')) || 0;
  const limit = Number(url.searchParams.get('limit')) || 20;

  const categories = await getCategoryList({ cursor, limit });
  const timestamp = serverTimestamp();

  return sendJson(
    res,
    200,
    categories.map((category) => ({
      ...category,
      updated_at: timestamp,
      created_at: timestamp,
    }))
  );
}
