import type { Locale } from "@/lib/i18n";
import { defaultLocale } from "@/lib/i18n";
import type { Tint } from "@/lib/blog/types";

/** A localized text field — English is required, other locales optional. */
type Localized = { en: string } & Partial<Record<Locale, string>>;

/** Category registry — slug is the canonical URL segment. */
export type BlogCategory = {
  slug: string;
  /**
   * Display name. The flat `name`/`blurb` fields are English defaults kept for
   * existing callers; localized variants live in `i18n.name`/`i18n.blurb`.
   * Prefer {@link getCategoryName}/{@link getCategoryBlurb} when a locale is
   * available.
   */
  name: string;
  tint: Tint;
  /** cat-tag colour key (matches the design's data-c). */
  c: Tint;
  /** Short marketing blurb used on the category page + topic rows. */
  blurb: string;
  /** Localized labels (en/fr). EN mirrors the flat `name`/`blurb`. */
  i18n: { name: Localized; blurb: Localized };
};

export const blogCategories: BlogCategory[] = [
  {
    slug: "seo",
    name: "SEO",
    tint: "blue",
    c: "blue",
    blurb:
      "Speed, crawlability and clean architecture: the foundations rankings are built on.",
    i18n: {
      name: { en: "SEO", fr: "SEO" },
      blurb: {
        en: "Speed, crawlability and clean architecture: the foundations rankings are built on.",
        fr: "Vitesse, crawlabilité et architecture propre : les fondations sur lesquelles se construit le positionnement.",
      },
    },
  },
  {
    slug: "ai",
    name: "AI Search",
    tint: "violet",
    c: "violet",
    blurb:
      "Generative engines are the new front page. Here's how to show up, and measure it.",
    i18n: {
      name: { en: "AI Search", fr: "Recherche IA" },
      blurb: {
        en: "Generative engines are the new front page. Here's how to show up, and measure it.",
        fr: "Les moteurs génératifs sont la nouvelle page d'accueil. Voici comment y apparaître, et le mesurer.",
      },
    },
  },
  {
    slug: "wordpress",
    name: "WordPress",
    tint: "coral",
    c: "coral",
    blurb:
      "Get the most out of your WordPress stack without fighting plugins and bloat.",
    i18n: {
      name: { en: "WordPress", fr: "WordPress" },
      blurb: {
        en: "Get the most out of your WordPress stack without fighting plugins and bloat.",
        fr: "Tirez le meilleur de votre stack WordPress sans vous battre contre les extensions et la surcharge.",
      },
    },
  },
  {
    slug: "headless",
    name: "Headless",
    tint: "mint",
    c: "mint",
    blurb:
      "Decoupled architectures that stay fast, crawlable and SEO-friendly.",
    i18n: {
      name: { en: "Headless", fr: "Headless" },
      blurb: {
        en: "Decoupled architectures that stay fast, crawlable and SEO-friendly.",
        fr: "Des architectures découplées qui restent rapides, crawlables et SEO-friendly.",
      },
    },
  },
  {
    slug: "cro",
    name: "CRO",
    tint: "amber",
    c: "amber",
    blurb:
      "Turn the traffic you already earn into customers: tested, never guessed.",
    i18n: {
      name: { en: "CRO", fr: "CRO" },
      blurb: {
        en: "Turn the traffic you already earn into customers: tested, never guessed.",
        fr: "Transformez le trafic que vous gagnez déjà en clients : testé, jamais deviné.",
      },
    },
  },
];

export const blogCategorySlugs = blogCategories.map((c) => c.slug);

export function getCategory(slug: string): BlogCategory | undefined {
  return blogCategories.find((c) => c.slug === slug);
}

/** Resolve a localized field, falling back to EN (default locale). */
function localize(field: Localized, lang: Locale = defaultLocale): string {
  return field[lang] ?? field.en;
}

/** Localized category name; defaults to English when no locale is given. */
export function getCategoryName(
  category: BlogCategory,
  lang: Locale = defaultLocale,
): string {
  return localize(category.i18n.name, lang);
}

/** Localized category blurb; defaults to English when no locale is given. */
export function getCategoryBlurb(
  category: BlogCategory,
  lang: Locale = defaultLocale,
): string {
  return localize(category.i18n.blurb, lang);
}
