import { Metadata } from 'next';
import { setRequestLocale } from 'next-intl/server';
import { notFound } from 'next/navigation';
import { getMdxContent } from '@/lib/mdx';
import { MDXRemote } from 'next-mdx-remote/rsc';

export const dynamic = 'force-dynamic';

// Import components used in MDX
import { HeroVideo as HomeHero } from '@/components/blocks/HeroVideo';
import { SubCompanyTiles as HomeSubCompanyTiles } from '@/components/blocks/SubCompanyTiles';
import { CompetenceBentoGrid as HomeCompetenceBentoGrid } from '@/components/blocks/CompetenceBentoGrid';
import { ReferencesSlider as HomeReferencesSlider } from '@/components/blocks/ReferencesSlider';
import { FaqBlock } from '@/components/blocks/FaqBlock';
import JsonLd from '@/components/JsonLd';

import { Button } from '@/components/ui/Button';
import { Heading } from '@/components/ui/Heading';

const mdxComponents = {
  HomeHero,
  HomeSubCompanyTiles,
  HomeCompetenceBentoGrid,
  HomeReferencesSlider,
  FaqBlock,
  JsonLd,
  Button,
  Heading,
};

export async function generateMetadata(props: any): Promise<Metadata> {
  const { locale } = await props.params;
  if (locale !== 'de' && locale !== 'en') return {};
  
  const mdx = await getMdxContent(locale, 'home');
  
  return {
    title: mdx?.frontmatter?.title || (locale === 'de' ? 'Startseite' : 'Home'),
    description: mdx?.frontmatter?.description,
  };
}

export default async function Home(props: any) {
  const { locale } = await props.params;
  
  if (locale !== 'de' && locale !== 'en') {
    notFound();
  }
  
  setRequestLocale(locale);

  const mdx = await getMdxContent(locale, 'home');

  if (!mdx) {
    notFound();
  }

  return (
    <main>
      <MDXRemote source={mdx.content} components={mdxComponents} />
    </main>
  );
}

