import { redirect } from "next/navigation"
import { desc, eq } from "drizzle-orm"
import { db } from "@/lib/db"
import { ai_recommendations } from "@/lib/db/schema"
import { getSessionUser } from "@/lib/session"
import { getEffectiveOwnerId } from "@/lib/account"
import { RecommendationsClient } from "./recommendations-client"

export const metadata = { title: "AI Recommendations" }

export default async function RecommendationsPage() {
  const user = await getSessionUser()
  if (!user) redirect("/login")

  const ownerId = await getEffectiveOwnerId(user.id)

  const recommendations = await db
    .select()
    .from(ai_recommendations)
    .where(eq(ai_recommendations.user_id, ownerId))
    .orderBy(desc(ai_recommendations.created_at))

  return <RecommendationsClient recommendations={recommendations ?? []} />
}
