export const dynamic = 'force-dynamic';
export const revalidate = 0;

import React from 'react';
import type {Metadata} from 'next';
import Link from 'next/link';
import {notFound} from 'next/navigation';
import {NextIntlClientProvider} from 'next-intl';
import {FaArrowRight, FaLayerGroup} from 'react-icons/fa';

import initTranslations from '@/i18n';
import HeroSection from '@/components/Homepage/HeroSection';
import UpcomingEventsSection from '@/components/Homepage/UpcomingEventsSection';
import MapSection from '@/components/Homepage/MapSection';
import MagSection from '@/components/Homepage/MagSection';
import CommunitySection from '@/components/Homepage/CommunitySection';
import {getDomaines} from '@/services/domaineService';
import {documentTitle} from '@/utils/documentTitle';

type RouteProps = {params: Promise<{locale: string; dprId: string}>};

async function domaineOr404(dprId: string) {
  const domaine = (await getDomaines()).find((d) => String(d.dprId) === dprId);
  if (!domaine) {
    notFound();
  }
  return domaine;
}

export async function generateMetadata({params}: RouteProps): Promise<Metadata> {
  const {dprId} = await params;
  const domaine = (await getDomaines()).find((d) => String(d.dprId) === dprId);

  return {title: documentTitle(domaine?.dprNom)};
}

/** Accueil d'un domaine (spec L77) : la page d'accueil filtrée sur un domaine de pratique. */
export default async function Page({params}: RouteProps) {
  const {locale, dprId} = await params;
  const domaine = await domaineOr404(dprId);
  const [{messages}, domaines] = await Promise.all([initTranslations({locale}), getDomaines()]);
  const t = (messages?.DomainePage ?? {}) as Record<string, string>;
  const texte = (cle: string, defaut: string) => (t[cle] || defaut).replace('{domaine}', domaine.dprNom);
  const agendaHref = `/${locale}/evenements?tev=${domaine.dprId}`;

  return (
    <NextIntlClientProvider locale={locale} messages={messages}>
      <section className="tk-section tk-domaine-head" data-testid="domaine-head">
        <div className="container">
          <span className="tk-badge tk-badge-warning mb-3">
            <FaLayerGroup className="me-2" aria-hidden="true" />
            {t.badge || 'Domaine'}
          </span>
          <h1 className="tk-magpage__title">{texte('title', 'Accueil {domaine}')}</h1>
          <p className="tk-magpage__subtitle">{texte('intro', 'Tout ce qui bouge autour de {domaine}.')}</p>
          <Link href={agendaHref} className="tk-btn tk-btn-primary">
            {texte('see_all', "Tout l'agenda {domaine}")}
            <FaArrowRight className="ms-2" aria-hidden="true" />
          </Link>
        </div>
      </section>
      <HeroSection locale={locale} dpr={domaines} domaineId={domaine.dprId} />
      <UpcomingEventsSection
        locale={locale}
        domaineId={domaine.dprId}
        seeAllHref={agendaHref}
        title={texte('upcoming_title', 'Prochains événements {domaine}')}
      />
      <MapSection locale={locale} />
      <MagSection locale={locale} />
      <CommunitySection locale={locale} />
    </NextIntlClientProvider>
  );
}
