import type { Metadata } from 'next';
import { notFound } from 'next/navigation';
import { DuAnDetailClient } from '@/components/du-an/du-an-detail-client';
import { fetchProjectBySlug } from '@/lib/du-an-server';
import { isResidentialProjectsEnabledServer } from '@/lib/feature-flags/residential-projects';

interface PageProps {
  params: Promise<{ slug: string; locale: string }>;
}

export async function generateMetadata({ params }: PageProps): Promise<Metadata> {
  if (!isResidentialProjectsEnabledServer()) return { title: 'Không tìm thấy dự án' };

  const { slug } = await params;
  const project = await fetchProjectBySlug(slug);
  if (!project) return { title: 'Không tìm thấy dự án' };

  return {
    title: `${project.name} — ${project.developer.name}`,
    description: project.description?.slice(0, 160),
    openGraph: {
      title: project.name,
      description: project.description?.slice(0, 160),
      images: project.media
        .filter((m) => m.type === 'image')
        .slice(0, 1)
        .map((m) => ({ url: m.url })),
    },
  };
}

export default async function DuAnDetailPage({ params }: PageProps) {
  if (!isResidentialProjectsEnabledServer()) {
    notFound();
  }

  const { slug } = await params;
  const project = await fetchProjectBySlug(slug);

  if (!project) {
    notFound();
  }

  return <DuAnDetailClient project={project} />;
}
