'use client';

import { ArrowRight } from 'lucide-react';

import CreatorCard from '@/components/creators/CreatorCard';
import { Button } from '@/components/ui/button';
import { Text } from '@/components/ui/typography';

import { Link } from '@/i18n/navigation';

import { formatNumber, useRandomSample } from './charts';

type Person = {
  id: string;
  name: string;
  profession: string | null;
  image: number | null;
  died: string | null;
};

/** `sentence`: the sentence of the student's pályakép that names the teacher. */
export type MentorPair = { teacher: Person; student: Person; sentence: string };

// Three rows of three on a wide screen, drawn at random; another draw on request.
const SAMPLE = 9;

// A text fragment that opens the student's pályakép with the sentence marked. Beyond
// encodeURIComponent, the fragment syntax needs "-" encoded; the rest as the review list has it.
const textFragment = (sentence: string) =>
  `:~:text=${encodeURIComponent(sentence).replace(
    /[-()!*']/g,
    (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`
  )}`;

const card = (person: Person) => ({
  alkotoAzonosito: person.id,
  nev: person.name,
  szakma: person.profession,
  elhalalozasIdeje: person.died,
  profilkep: person.image ? { id: person.image } : null,
});

/**
 * Master and student, one pair at a time: the two portraits with a strong arrow between them. The
 * whole card opens the student's pályakép, where the relation can be checked; the page's
 * openDetailsInNewTab sends it to a new tab, a full load, so the browser marks the sentence.
 */
export default function MentorPairs({ pairs }: { pairs: MentorPair[] }) {
  const [picked, redraw] = useRandomSample(pairs, SAMPLE);
  return (
    <div>
      <ul className="grid gap-4 sm:grid-cols-2 xl:grid-cols-3">
        {picked.map((pair) => (
          <li key={`${pair.teacher.id}-${pair.student.id}`}>
            <Link
              href={{
                pathname: '/alkoto/[id]/palyakep',
                params: { id: pair.student.id },
                hash: textFragment(pair.sentence),
              }}
              aria-label={`Forrás: ${pair.student.name} pályaképe`}
              className="group grid h-full grid-cols-[minmax(0,1fr)_auto_minmax(0,1fr)] items-center gap-2 border border-[#151720]/10 bg-white p-4 transition hover:border-mma-cyan focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#151720]"
            >
              <div className="mx-auto w-full max-w-[150px]">
                <CreatorCard creator={card(pair.teacher)} linked={false} />
              </div>
              <ArrowRight className="size-12 text-[#151720]" strokeWidth={3} aria-hidden />
              <div className="mx-auto w-full max-w-[150px]">
                <CreatorCard creator={card(pair.student)} linked={false} />
              </div>
            </Link>
          </li>
        ))}
      </ul>
      <div className="mt-4 flex flex-wrap items-center gap-4">
        <Button onClick={redraw}>Mutasson másokat</Button>
        <Text variant="meta" tone="muted" className="tabular-nums">
          Véletlenszerű válogatás a {formatNumber(pairs.length)} kapcsolatból
        </Text>
      </div>
    </div>
  );
}
