import { notFound } from 'next/navigation';
import { getPageBySlug, getAllPages, getMediaById } from '@/lib/data';
import { Metadata } from 'next';
import { SEO } from '@/components/SEO';
import { processHTML } from '@/lib/html-compat';
import { LocaleSwitcher } from '@/components/LocaleSwitcher';
import Link from 'next/link';

interface PageProps {
  params: {
    locale: string;
    slug?: string;
  };
}

export async function generateStaticParams() {
  const pages = await getAllPages();
  const params = pages.map((page) => ({
    locale: page.locale,
    slug: page.slug,
  }));
  return params;
}

export async function generateMetadata({ params }: PageProps): Promise<Metadata> {
  const { locale, slug = 'home' } = params;
  
  // Map root path to actual home page slugs
  const homeSlugs: Record<string, string> = {
    'en': 'corporate-3-landing-2',
    'de': 'start'
  };
  
  const actualSlug = slug === 'home' ? homeSlugs[locale] || 'home' : slug;
  const page = await getPageBySlug(actualSlug, locale);

  if (!page) {
    return {
      title: 'Page Not Found',
    };
  }

  return {
    title: page.title,
    description: page.excerptHtml || '',
    alternates: {
      languages: {
        de: slug === 'home' ? '/de' : `/de/${slug}`,
        en: slug === 'home' ? '/en' : `/en/${slug}`,
      },
    },
  };
}

export default async function Page({ params }: PageProps) {
  const { locale, slug = 'home' } = params;
  
  // Map root path to actual home page slugs
  const homeSlugs: Record<string, string> = {
    'en': 'corporate-3-landing-2',
    'de': 'start'
  };
  
  const actualSlug = slug === 'home' ? homeSlugs[locale] || 'home' : slug;
  const page = await getPageBySlug(actualSlug, locale);

  if (!page) {
    notFound();
  }

  // Use contentHtml if available, otherwise use excerptHtml
  const contentToDisplay = page.contentHtml && page.contentHtml.trim() !== '' 
    ? page.contentHtml 
    : page.excerptHtml;
  
  const processedContent = processHTML(contentToDisplay || '');

  // Get featured image if available
  const featuredImage = page.featuredImage ? getMediaById(page.featuredImage) : null;

  return (
    <>
      <SEO
        title={page.title}
        description={page.excerptHtml || ''}
      />
      
      {/* Hero Section with Featured Image */}
      {featuredImage && (
        <div className="relative h-64 md:h-96 bg-gray-200">
          <img
            src={featuredImage.localPath}
            alt={page.title}
            className="w-full h-full object-cover"
          />
          <div className="absolute inset-0 bg-black bg-opacity-40"></div>
          <div className="absolute inset-0 flex items-center justify-center text-center">
            <h1 className="text-4xl md:text-6xl font-bold text-white drop-shadow-lg">
              {page.title}
            </h1>
          </div>
        </div>
      )}

      {/* Main Content */}
      <main className="container mx-auto px-4 py-8">
        {!featuredImage && (
          <div className="max-w-4xl mx-auto mb-8">
            <h1 className="text-4xl font-bold text-gray-900 mb-4">
              {page.title}
            </h1>
            {page.excerptHtml && (
              <div 
                className="text-xl text-gray-600 leading-relaxed"
                dangerouslySetInnerHTML={{ __html: processHTML(page.excerptHtml) }}
              />
            )}
          </div>
        )}

        {processedContent && (
          <div className="max-w-4xl mx-auto bg-white rounded-lg shadow-sm p-8">
            <div 
              className="prose prose-lg max-w-none"
              dangerouslySetInnerHTML={{ __html: processedContent }}
            />
          </div>
        )}

        {/* Navigation Links */}
        <div className="max-w-4xl mx-auto mt-12">
          <div className="grid grid-cols-2 md:grid-cols-4 gap-4">
            <Link 
              href={`/${locale}/blog`}
              className="p-4 bg-blue-50 hover:bg-blue-100 rounded-lg text-center transition-colors"
            >
              <div className="font-semibold text-blue-900">Blog</div>
              <div className="text-sm text-blue-700">Read our latest posts</div>
            </Link>
            <Link 
              href={`/${locale}/products`}
              className="p-4 bg-green-50 hover:bg-green-100 rounded-lg text-center transition-colors"
            >
              <div className="font-semibold text-green-900">Products</div>
              <div className="text-sm text-green-700">Browse our catalog</div>
            </Link>
            <Link 
              href={`/${locale}/contact`}
              className="p-4 bg-orange-50 hover:bg-orange-100 rounded-lg text-center transition-colors"
            >
              <div className="font-semibold text-orange-900">Contact</div>
              <div className="text-sm text-orange-700">Get in touch</div>
            </Link>
            <Link 
              href={`/${locale}/blog`}
              className="p-4 bg-purple-50 hover:bg-purple-100 rounded-lg text-center transition-colors"
            >
              <div className="font-semibold text-purple-900">News</div>
              <div className="text-sm text-purple-700">Latest updates</div>
            </Link>
          </div>
        </div>
      </main>

      {/* Locale Switcher */}
      <div className="bg-gray-50 py-8">
        <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 text-center">
          <LocaleSwitcher />
        </div>
      </div>
    </>
  );
}