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 any) || {}

  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}
      optimizedLineProfiler={metadata.optimizedLineProfiler}
      originalRuntime={metadata.prCommentFields?.original_runtime}
      bestRuntime={metadata.prCommentFields?.best_runtime}
    />
  )
}
