// Archive is legacy/test data only — not part of the live signal stack.
// noindex keeps it out of search results (duplicate/thin content risk)
// while keeping it accessible to logged-in users for inspection.
export const revalidate = 60  // archive data rarely changes — ISR at 60s keeps server load low
import type { Metadata } from 'next'
import { type PostListResponse } from '@/lib/api'
import { getPosts } from '@/lib/api'
import { buildArchiveFallbackResponse, getInitialPostPage } from '@/lib/postPage'
import ArchivePageClient from './ArchivePageClient'

export const metadata: Metadata = {
  robots: { index: false, follow: false },
}

export default async function ArchivePage() {
  const initialData: PostListResponse | null = await getInitialPostPage(30, 1, {
    filters: { archiveOnly: true },
    legacyFallback: async () => {
      // Old backend (no /posts-paged archive_only): /posts caps at 500 per
      // page, so a single page only sees the latest 500 posts globally — older
      // archive rows get buried under fresh truth posts and silently dropped.
      // Page through a few windows so archive coverage isn't truncated.
      const MAX_PAGES = 4   // up to 2000 most-recent posts scanned for archive rows
      const collected: Awaited<ReturnType<typeof getPosts>> = []
      for (let p = 1; p <= MAX_PAGES; p++) {
        const batch = await getPosts(500, p).catch(() => null)
        if (!batch || batch.length === 0) break
        collected.push(...batch)
        if (batch.length < 500) break   // last page reached
      }
      return collected.length ? buildArchiveFallbackResponse(collected, 1, 30) : null
    },
  })

  return <ArchivePageClient initialData={initialData} />
}
