export interface Author {
  name: string;
  slug: string;
  avatar: string;
  bio: string;
  jobTitle: string;
  linkedin?: string;
}

import { siteConfig } from "@/lib/site";

const SITE_URL = siteConfig.url;

export const authors: Record<string, Author> = {
  "joel-lassalle": {
    name: "Joël Lassalle",
    slug: "joel-lassalle",
    avatar: "/blog/joel_lassalle.webp",
    bio: "Ingénieur énergie et fondateur d'Acieb Énergie. Expert en optimisation de contrats d'électricité et de gaz pour les professionnels.",
    jobTitle: "Fondateur & Ingénieur Énergie",
    linkedin: "https://www.linkedin.com/in/joel-lassalle/",
  },
};

/**
 * Resolve author by id or name. Falls back to default author.
 */
export function getAuthor(idOrName?: string): Author {
  if (!idOrName) return authors["joel-lassalle"];

  // Direct ID match
  if (authors[idOrName]) return authors[idOrName];

  // Match by name (for existing MDX using author: "Joel Lassalle")
  const byName = Object.values(authors).find(
    (a) => a.name.toLowerCase() === idOrName.toLowerCase(),
  );
  return byName ?? authors["joel-lassalle"];
}

/**
 * Build JSON-LD Person object for E-E-A-T
 */
export function authorJsonLd(author: Author) {
  return {
    "@type": "Person",
    name: author.name,
    url: author.linkedin ?? `${SITE_URL}/`,
    image: author.avatar.startsWith("http")
      ? author.avatar
      : `${SITE_URL}${author.avatar}`,
    jobTitle: author.jobTitle,
    worksFor: { "@id": `${SITE_URL}/#organization` },
    ...(author.linkedin ? { sameAs: [author.linkedin] } : {}),
  };
}
