import { libelleStatut, traducteur, type Langue } from '@notariat-ci/i18n';
import {
  Button,
  Card,
  Input,
  Modal,
  SceauPlaceholder,
  Select,
  StatusBadge,
  STATUTS_NOTAIRE,
  Table,
  ToastProvider,
  Tooltip,
  useToast,
  type StatutNotaire,
  type Theme,
} from '@notariat-ci/ui';
import { useState } from 'react';

interface NotaireFictif {
  nom: string;
  ville: string;
  statut: StatutNotaire;
  /** Civilité « Madame » : le statut s'accorde au féminin (Q-011). */
  feminin: boolean;
}

function Section({ titre, children }: { titre: string; children: React.ReactNode }) {
  return (
    <section className="flex flex-col gap-3">
      <h3 className="text-xl font-semibold">{titre}</h3>
      {children}
    </section>
  );
}

function Panneau({ theme, langue }: { theme: Theme; langue: Langue }) {
  const t = traducteur(langue);
  const [fenetreOuverte, setFenetreOuverte] = useState(false);
  const afficherNotification = useToast();

  const notaires: NotaireFictif[] = [
    { nom: t('demo.notaire_a'), ville: t('demo.ville_a'), statut: 'en_exercice', feminin: false },
    { nom: t('demo.notaire_b'), ville: t('demo.ville_b'), statut: 'suspendu', feminin: true },
    { nom: t('demo.notaire_c'), ville: t('demo.ville_a'), statut: 'honoraire', feminin: false },
    { nom: t('demo.notaire_d'), ville: t('demo.ville_b'), statut: 'decede', feminin: true },
  ];

  return (
    <div data-theme={theme} className="flex flex-col gap-8 rounded-xl bg-fond p-6 text-texte">
      <h2 className="text-2xl font-semibold">
        {theme === 'clair' ? t('demo.theme_clair') : t('demo.theme_sombre')}
      </h2>

      <Section titre={t('demo.section_boutons')}>
        <div className="flex flex-wrap gap-3">
          <Button>{t('demo.bouton_primaire')}</Button>
          <Button variant="secondaire">{t('demo.bouton_secondaire')}</Button>
          <Button variant="discret">{t('demo.bouton_discret')}</Button>
          <Button variant="alerte">{t('demo.bouton_alerte')}</Button>
          <Button disabled>{t('demo.bouton_desactive')}</Button>
        </div>
      </Section>

      <Section titre={t('demo.section_formulaire')}>
        <div className="grid gap-4 sm:grid-cols-2">
          <Input
            label={t('demo.champ_nom')}
            hint={t('demo.champ_nom_aide')}
            required
            requiredLabel={t('commun.champ_obligatoire')}
            autoComplete="off"
          />
          <Select
            label={t('demo.champ_ville')}
            placeholder={t('demo.choix_ville')}
            error={t('demo.champ_ville_erreur')}
            options={[
              { value: 'a', label: t('demo.ville_a') },
              { value: 'b', label: t('demo.ville_b') },
            ]}
          />
        </div>
      </Section>

      <Section titre={t('demo.section_badges')}>
        {[false, true].map((feminin) => (
          <div key={String(feminin)} className="flex flex-col gap-2">
            <p className="text-sm text-texte-doux">
              {feminin ? t('demo.forme_feminine') : t('demo.forme_masculine')}
            </p>
            <div className="flex flex-wrap gap-3">
              {STATUTS_NOTAIRE.map((statut) => (
                <StatusBadge
                  key={statut}
                  status={statut}
                  label={libelleStatut(langue, statut, feminin)}
                />
              ))}
            </div>
          </div>
        ))}
      </Section>

      <Section titre={t('demo.section_carte')}>
        <Card title={t('demo.carte_titre')} headingLevel={4}>
          <p>{t('demo.carte_texte')}</p>
        </Card>
      </Section>

      <Section titre={t('demo.section_tableau')}>
        <Table
          caption={t('demo.tableau_titre')}
          scrollLabel={t('commun.tableau_defilable', { titre: t('demo.tableau_titre') })}
          rows={notaires}
          rowKey={(notaire) => notaire.nom}
          columns={[
            { key: 'nom', header: t('demo.colonne_nom'), render: (notaire) => notaire.nom },
            { key: 'ville', header: t('demo.colonne_ville'), render: (notaire) => notaire.ville },
            {
              key: 'statut',
              header: t('demo.colonne_statut'),
              render: (notaire) => (
                <StatusBadge
                  status={notaire.statut}
                  label={libelleStatut(langue, notaire.statut, notaire.feminin)}
                />
              ),
            },
          ]}
        />
      </Section>

      <Section titre={t('demo.section_interactions')}>
        <div className="flex flex-wrap gap-3">
          <Button variant="secondaire" onClick={() => setFenetreOuverte(true)}>
            {t('demo.ouvrir_fenetre')}
          </Button>
          <Button
            variant="secondaire"
            onClick={() => afficherNotification(t('demo.notification_texte'))}
          >
            {t('demo.afficher_notification')}
          </Button>
        </div>
        <p>
          {t('demo.infobulle_phrase_avant')}{' '}
          <Tooltip term={t('demo.infobulle_terme')} definition={t('demo.infobulle_definition')} />{' '}
          {t('demo.infobulle_phrase_apres')}
        </p>
        <Modal
          open={fenetreOuverte}
          onClose={() => setFenetreOuverte(false)}
          title={t('demo.fenetre_titre')}
          closeLabel={t('commun.fermer')}
        >
          <p>{t('demo.fenetre_texte')}</p>
        </Modal>
      </Section>

      <Section titre={t('demo.section_sceau')}>
        <SceauPlaceholder size={120} label={t('commun.sceau_chambre')} />
      </Section>
    </div>
  );
}

/** Vitrine du design system : tous les composants, dans le thème clair puis le thème sombre. */
export default function Vitrine({ langue }: { langue: Langue }) {
  const t = traducteur(langue);
  return (
    <ToastProvider regionLabel={t('commun.notifications')} closeLabel={t('commun.fermer')}>
      <div className="grid gap-8 xl:grid-cols-2">
        <Panneau theme="clair" langue={langue} />
        <Panneau theme="sombre" langue={langue} />
      </div>
    </ToastProvider>
  );
}
