// app/[locale]/about/page.tsx
export const dynamic = 'force-dynamic';
import { AboutContent } from '@/features/about/ui/AboutContent';
import { AboutHero } from '@/features/about/ui/AboutHero';
import { PartnershipForm } from '@/features/about/ui/AboutPage';
import { BASE_URL } from '@/shared/config/api/URLs';
import { Metadata } from 'next';

function getImageUrl(image?: string | null): string {
  if (!image) return '/placeholder.svg';
  return image.includes(BASE_URL) ? image : BASE_URL + image;
}

const fetchAboutData = async () => {
  const res = await fetch(
    'https://api.gastro.felixits.uz/api/v1/shared/aboutus/',
    { cache: 'no-store' },
  );
  if (!res.ok) throw new Error(`Failed to fetch about data: ${res.status}`);
  const data = await res.json();
  return data.banner;
};

interface Props {
  params: Promise<{ locale: 'uz' | 'ru' | 'en' }>;
}

export async function generateMetadata({ params }: Props): Promise<Metadata> {
  const { locale } = await params;

  try {
    const banner = await fetchAboutData();

    const title =
      locale === 'uz'
        ? banner.title_uz
        : locale === 'ru'
          ? banner.title_ru
          : banner.title_en;

    const description =
      locale === 'uz'
        ? banner.description_uz
        : locale === 'ru'
          ? banner.description_ru
          : banner.description_en;

    return {
      title: title || 'Gastro Market',
      description: description || 'Gastro Market mahsulotlarini kashf eting',
      openGraph: {
        title: title || 'Gastro Market',
        description: description || 'Gastro Market mahsulotlarini kashf eting',
        type: 'website',
        images: [
          {
            url: getImageUrl(banner.image),
            width: 1200,
            height: 630,
            alt: title,
          },
        ],
      },
      twitter: {
        card: 'summary_large_image',
        title: title || 'Gastro Market',
        description: description || 'Gastro Market mahsulotlarini kashf eting',
        images: [getImageUrl(banner.image)],
      },
    };
  } catch (err) {
    console.error('About metadata error:', err);
    return {
      title: 'Gastro Market',
      description: 'Gastro Market mahsulotlarini kashf eting',
    };
  }
}

const AboutPage = async () => {
  return (
    <div className="custom-container">
      <AboutHero />
      <AboutContent />
      <PartnershipForm />
    </div>
  );
};

export default AboutPage;
