---
/**
 * Pages institutionnelles (mentions légales, confidentialité, accessibilité, contact).
 * Le texte est dans @notariat-ci/i18n (clés page.<page>.sN.titre / .texte).
 * Les coordonnées et mentions de la Chambre sont des données paramétrables par le bureau
 * (public.institution, Q-014) ; une valeur absente s'affiche « À compléter par la Chambre ».
 */
import type { Institution } from '@notariat-ci/annuaire';
import { lienTelephone } from '@notariat-ci/annuaire';
import { traducteur, type Cle, type Langue } from '@notariat-ci/i18n';
import Base from '../layouts/Base.astro';
import { donnees } from '../lib/donnees';
import { PAGES_TEXTE, type PageTexte } from '../lib/pages';

interface Props {
  langue: Langue;
  page: PageTexte;
}

const { langue, page } = Astro.props;
const t = traducteur(langue);
const racine = `page.${page.replaceAll('-', '_')}`;
const cle = (suffixe: string) => `${racine}.${suffixe}` as Cle;
const sections = Array.from({ length: PAGES_TEXTE[page] }, (_, i) => i + 1);
const { institution } = await donnees();

interface Champ {
  cle: keyof Institution;
  libelle: Cle;
  lien?: 'tel' | 'mail';
}
const CHAMPS: Partial<Record<PageTexte, readonly Champ[]>> = {
  'mentions-legales': [
    { cle: 'identifiantsOfficiels', libelle: 'institution.identifiants' },
    { cle: 'adresse', libelle: 'institution.adresse' },
    { cle: 'boitePostale', libelle: 'institution.boite_postale' },
    { cle: 'ville', libelle: 'institution.ville' },
    { cle: 'directeurPublication', libelle: 'institution.directeur' },
    { cle: 'hebergeurPortail', libelle: 'institution.hebergeur_portail' },
    { cle: 'hebergeurDonnees', libelle: 'institution.hebergeur_donnees' },
  ],
  contact: [
    { cle: 'adresse', libelle: 'institution.adresse' },
    { cle: 'boitePostale', libelle: 'institution.boite_postale' },
    { cle: 'ville', libelle: 'institution.ville' },
    { cle: 'telephone', libelle: 'institution.telephone', lien: 'tel' },
    { cle: 'courriel', libelle: 'institution.courriel', lien: 'mail' },
  ],
};
const champs = CHAMPS[page] ?? [];
const lien = 'text-lien underline underline-offset-4 break-all';
---

<Base langue={langue} titre={t(cle('titre'))} description={t(cle('description'))}>
  <h1 class="mb-2 text-4xl font-semibold">{t(cle('titre'))}</h1>
  <p class="mb-8 max-w-prose text-lg">{t(cle('description'))}</p>
  <div class="flex max-w-prose flex-col gap-6">
    {champs.length > 0 && (
      <section
        aria-labelledby="institution"
        class="rounded-lg border border-bordure bg-surface p-5"
      >
        <h2 id="institution" class="mb-3 text-2xl font-semibold">
          {t('institution.titre')}
        </h2>
        <dl class="grid gap-x-6 gap-y-2 sm:grid-cols-[auto_1fr]">
          {champs.map((champ) => {
            const valeur = institution[champ.cle];
            return (
              <>
                <dt class="font-semibold">{t(champ.libelle)}</dt>
                <dd>
                  {valeur === null ? (
                    <span class="text-texte-doux italic">{t('institution.a_completer')}</span>
                  ) : champ.lien === 'tel' ? (
                    <a class={lien} href={lienTelephone(valeur)}>
                      {valeur}
                    </a>
                  ) : champ.lien === 'mail' ? (
                    <a class={lien} href={`mailto:${valeur}`}>
                      {valeur}
                    </a>
                  ) : (
                    valeur
                  )}
                </dd>
              </>
            );
          })}
        </dl>
      </section>
    )}
    {sections.map((n) => (
      <section>
        <h2 class="mb-2 text-2xl font-semibold">{t(cle(`s${n}.titre`))}</h2>
        <p>{t(cle(`s${n}.texte`))}</p>
      </section>
    ))}
  </div>
</Base>
