import { notFound, redirect } from "next/navigation"
import { getReviewPageInitData } from "../action"
import { ProfilerClient } from "./profiler-client"

interface ProfilerPageProps {
  params: Promise<{ traceId: string }>
}

export default async function LineProfilerPage({ params }: ProfilerPageProps) {
  const { traceId } = await params

  const initData = await getReviewPageInitData(traceId)

  if (!initData) {
    redirect("/login")
  }

  if (!initData.event) {
    notFound()
  }

  const metadata = (initData.event.metadata as Record<string, unknown> | null) ?? {}
  const prCommentFields = (metadata.prCommentFields as Record<string, unknown> | undefined) ?? {}

  return (
    <ProfilerClient
      traceId={traceId}
      functionName={initData.event.function_name ?? null}
      filePath={initData.event.file_path ?? null}
      speedupX={initData.event.speedup_x ?? null}
      originalLineProfiler={metadata.originalLineProfiler as string | undefined}
      optimizedLineProfiler={metadata.optimizedLineProfiler as string | undefined}
      originalRuntime={prCommentFields.original_runtime as string | undefined}
      bestRuntime={prCommentFields.best_runtime as string | undefined}
    />
  )
}
