import { redirect } from 'next/navigation'
import { getCurrentUser } from '@/lib/session'
import { listRunLogs } from '@/lib/worker-logs'
import type { RunLogSummary } from '@/lib/parse-worker-log'
import WorkerLogsView from './_components/worker-logs-view'

export const dynamic = 'force-dynamic'

export default async function WorkerLogsPage() {
  const user = await getCurrentUser()
  if (!user) redirect('/login')

  let initialLogs: RunLogSummary[] = []
  let initialError: string | null = null
  try {
    initialLogs = await listRunLogs(10)
  } catch (err) {
    initialError = err instanceof Error ? err.message : 'Failed to read worker logs'
  }

  return (
    <div className="min-h-screen bg-background p-6">
      <div className="mx-auto max-w-6xl space-y-6">
        <div>
          <h1 className="text-2xl font-semibold tracking-tight">Worker Logs</h1>
          <p className="text-sm text-muted-foreground">
            Recente runs van de Scrum4Me-worker — klik een rij voor de uitgewerkte timeline
          </p>
        </div>
        <WorkerLogsView initialLogs={initialLogs} initialError={initialError} />
      </div>
    </div>
  )
}
