import { Metadata } from 'next';
import { getTranslations } from 'next-intl/server';

import { getCreationList, getFeaturedCreations } from '@/services/creations';
import { getCreatorData, getCreatorLifeEvents, getCreatorList } from '@/services/creators';

import ParallelLifepathsClient from '@/app/[locale]/parhuzamos-eletutak/ParallelPathsClient';

export default async function ParallelLifepathsPage({
  params,
  searchParams,
}: {
  params: Promise<{ locale: string }>;
  searchParams: Promise<{ [key: string]: string }>;
}) {
  const awaitedParams = await params;
  const awaitedSearchParams = await searchParams;
  const locale = awaitedParams.locale;
  const t = await getTranslations('parallelPathsPage');

  // Get localized URL param names
  const leftParam = t('urlParams.left');
  const rightParam = t('urlParams.right');
  const categoryParam = t('urlParams.category');

  const leftId = awaitedSearchParams[leftParam] || null;
  const rightId = awaitedSearchParams[rightParam] || null;
  const rawCategory = awaitedSearchParams[categoryParam];
  const category = rawCategory === 'eletut' ? 'eletut' : 'alkotas';

  const sortLocale = locale.startsWith('en') ? 'en' : 'hu';
  const creatorName = (name: string | null, fallback: string) => (name || '').trim() || fallback;
  const nameList = (await getCreatorList()).sort((left, right) =>
    creatorName(left.nev, left.alkotoAzonosito).localeCompare(
      creatorName(right.nev, right.alkotoAzonosito),
      sortLocale
    )
  );

  const [creatorLeft, dataLeft, lifeLeft] = leftId
    ? await Promise.all([
        getCreatorData(leftId),
        locale === 'hu'
          ? getCreationList({ personIdentifierList: [leftId] })
          : getFeaturedCreations(leftId, 16),
        locale === 'hu' ? getCreatorLifeEvents(leftId) : null,
      ])
    : [null, null, null];

  const [creatorRight, dataRight, lifeRight] = rightId
    ? await Promise.all([
        getCreatorData(rightId),
        locale === 'hu'
          ? getCreationList({ personIdentifierList: [rightId] })
          : getFeaturedCreations(rightId, 16),
        locale === 'hu' ? getCreatorLifeEvents(rightId) : null,
      ])
    : [null, null, null];

  return (
    <ParallelLifepathsClient
      nameList={nameList}
      creatorLeft={creatorLeft}
      dataLeft={dataLeft}
      creatorRight={creatorRight}
      dataRight={dataRight}
      lifeLeft={lifeLeft}
      lifeRight={lifeRight}
      category={category}
      locale={locale}
    />
  );
}

// Metadata
export async function generateMetadata({
  searchParams,
}: {
  searchParams?: Promise<{ [key: string]: string | string[] | undefined }>;
}): Promise<Metadata> {
  const t = await getTranslations('parallelPathsPage.metaData');
  const tParams = await getTranslations('parallelPathsPage.urlParams');
  const leftParam = tParams('left');
  const rightParam = tParams('right');
  const categoryParam = tParams('category');
  const awaitedSearchParams = await searchParams;
  const hasSearchParam = Boolean(
    awaitedSearchParams?.[leftParam] ||
    awaitedSearchParams?.[rightParam] ||
    awaitedSearchParams?.[categoryParam]
  );

  return {
    title: t('title'),
    description: t('description'),
    alternates: {
      canonical: '/parhuzamos-eletutak',
    },
    robots: hasSearchParam ? { index: false, follow: true } : undefined,
    openGraph: {
      url: '/parhuzamos-eletutak',
      title: t('title'),
      description: t('description'),
      type: 'website',
      locale: 'hu_HU',
    },
    twitter: {
      card: 'summary_large_image',
      title: t('title'),
      description: t('description'),
    },
  };
}
