import { DashboardErrorBoundary } from "@/components/dashboard/DashboardErrorBoundary"
import { getRepoDetailInitData } from "./data"
import { RepoDetailClient } from "./repo-detail-client"
import { GitPullRequest } from "lucide-react"

export default async function RepositoryDetailPage({
  params,
}: {
  params: Promise<{ repositoryId: string }>
}) {
  const { repositoryId } = await params
  const initData = await getRepoDetailInitData(repositoryId)

  // No session — auth middleware will redirect
  if (!initData) {
    return null
  }

  // Repository not found
  if (!initData.repository || !initData.stats) {
    return (
      <div className="flex justify-center items-center min-h-[70vh] p-4">
        <div className="text-center">
          <div className="inline-flex items-center justify-center w-16 h-16 rounded-full bg-accent mb-4">
            <GitPullRequest size={32} className="text-muted-foreground" />
          </div>
          <h3 className="text-lg font-semibold mb-2 text-foreground">Repository not found</h3>
          <p className="text-sm text-muted-foreground">
            The repository you&apos;re looking for doesn&apos;t exist or you don&apos;t have access
            to it.
          </p>
        </div>
      </div>
    )
  }

  return (
    <DashboardErrorBoundary>
      <RepoDetailClient
        repositoryId={repositoryId}
        initialUserId={initData.userId}
        initialOrgId={initData.orgId ?? null}
        initialRepository={initData.repository as any}
        initialStats={initData.stats}
      />
    </DashboardErrorBoundary>
  )
}
