export const PRODUCT_CANONICAL_PATH_BY_SLUG = {
  covatherm: "/peinture-reflective-covatherm",
  "covatherm-light": "/laque-solaire-covatherm-light",
  "covaseal-20": "/covaseal",
  "covametal-20": "/covametal",
} as const;

export const INDUSTRY_CANONICAL_PATH_BY_SLUG = {
  logistique: "/logistique",
  distribution: "/distribution",
  tertiaire: "/tertiaire",
  collectivites: "/collectivité",
  agricole: "/industries/agricole",
  erp: "/industries/erp",
} as const;

export const REFERENCE_CANONICAL_PATH_BY_SLUG = {
  "roche-bobois-versailles": "/cool-roof-roche-bobois",
} as const;

const normalizePath = (path: string): string => {
  const decoded = path
    .split("/")
    .map((part) => {
      try {
        return decodeURIComponent(part);
      } catch {
        return part;
      }
    })
    .join("/");
  const trimmed = decoded.replace(/\/+$/, "");
  return trimmed === "" ? "/" : trimmed;
};

const reverse = <T extends Record<string, string>>(map: T): Record<string, string> =>
  Object.fromEntries(Object.entries(map).map(([slug, path]) => [normalizePath(path), slug]));

export const PRODUCT_SLUG_BY_PROD_PATH = reverse(PRODUCT_CANONICAL_PATH_BY_SLUG);
export const INDUSTRY_SLUG_BY_PROD_PATH = reverse(INDUSTRY_CANONICAL_PATH_BY_SLUG);
export const REFERENCE_SLUG_BY_PROD_PATH = reverse(REFERENCE_CANONICAL_PATH_BY_SLUG);

export function productCanonicalPath(slug: string): string {
  return PRODUCT_CANONICAL_PATH_BY_SLUG[slug as keyof typeof PRODUCT_CANONICAL_PATH_BY_SLUG] ?? `/solutions/${slug}`;
}

export function industryCanonicalPath(slug: string): string {
  return INDUSTRY_CANONICAL_PATH_BY_SLUG[slug as keyof typeof INDUSTRY_CANONICAL_PATH_BY_SLUG] ?? `/industries/${slug}`;
}

export function referenceCanonicalPath(slug: string): string {
  return REFERENCE_CANONICAL_PATH_BY_SLUG[slug as keyof typeof REFERENCE_CANONICAL_PATH_BY_SLUG] ?? `/references/${slug}`;
}

export function productSlugForProdPath(path: string): string | undefined {
  return PRODUCT_SLUG_BY_PROD_PATH[normalizePath(path)];
}

export function industrySlugForProdPath(path: string): string | undefined {
  return INDUSTRY_SLUG_BY_PROD_PATH[normalizePath(path)];
}

export function referenceSlugForProdPath(path: string): string | undefined {
  return REFERENCE_SLUG_BY_PROD_PATH[normalizePath(path)];
}

export const HUB_CONFIGS = {
  "aides-obligations": {
    title: "Aides et obligations pour vos travaux cool roof",
    description:
      "Guides Covalba sur les aides, primes CEE, obligations et leviers réglementaires liés au cool roof.",
    keywords: ["aide", "aides", "cee", "prime", "bat-en-112", "obligation", "décret", "reglementation", "réglementation"],
  },
  "chaleur-travail": {
    title: "Chaleur au travail : comprendre et agir",
    description:
      "Articles sur le confort thermique, les pics de chaleur, la climatisation et la protection des équipes.",
    keywords: ["chaleur", "travail", "confort", "climatisation", "canicule", "température", "temperature"],
  },
  etancheite: {
    title: "Étanchéité de toiture et cool roof",
    description:
      "Guides techniques sur l'étanchéité, les infiltrations, les fissures et les systèmes réflectifs pour toiture.",
    keywords: ["étanchéité", "etancheite", "infiltration", "fuite", "fissure", "covaseal", "membrane"],
  },
  isolation: {
    title: "Isolation, confort d'été et performance énergétique",
    description:
      "Ressources pour comparer isolation, cool roof et réduction des surchauffes dans les bâtiments professionnels.",
    keywords: ["isolation", "isolant", "énergie", "energie", "performance", "surchauffe", "confort d'été", "confort ete"],
  },
  peintures: {
    title: "Peintures réflectives et revêtements cool roof",
    description:
      "Articles Covalba pour choisir une peinture réflective durable, comprendre le SRI et comparer les solutions cool roof.",
    keywords: ["peinture", "peintures", "réflective", "reflective", "revêtement", "revetement", "sri", "cool roof", "covatherm"],
  },
} as const;

export type HubSlug = keyof typeof HUB_CONFIGS;

export function isHubSlug(slug: string): slug is HubSlug {
  return Object.prototype.hasOwnProperty.call(HUB_CONFIGS, slug);
}
