import { getCollection } from 'astro:content';

export type Entry = {
  type: 'review' | 'post' | 'finds';
  id: string;
  href: string;
  date: Date;
  category: string;
  tags: string[];
  primary: string;
  secondary?: string;
};

export async function getAllEntries(): Promise<Entry[]> {
  const reviews = await getCollection('reviews');
  const posts = await getCollection('posts');
  const findsPosts = await getCollection('finds');

  const r: Entry[] = reviews.map((e) => ({
    type: 'review',
    id: e.id,
    href: `/reviews/${e.id}/`,
    date: e.data.date,
    category: e.data.category,
    tags: e.data.tags,
    primary: e.data.name,
    secondary: e.data.subtitle,
  }));

  const p: Entry[] = posts.map((e) => ({
    type: 'post',
    id: e.id,
    href: `/posts/${e.id}/`,
    date: e.data.date,
    category: e.data.category,
    tags: e.data.tags,
    primary: e.data.title,
  }));

  const f: Entry[] = findsPosts.map((e) => ({
    type: 'finds',
    id: e.id,
    href: `/finds/${e.id}/`,
    date: e.data.date,
    category: e.data.category,
    tags: e.data.tags,
    primary: e.data.title,
    secondary: e.data.blurb,
  }));

  return [...r, ...p, ...f].sort((a, b) => b.date.valueOf() - a.date.valueOf());
}
