export const associationThematicValues = [
  "culture_loisirs",
  "social_sante",
  "education_formation",
  "economie_territoire",
  "environnement_patrimoine",
  "institutions_divers",
] as const;

export type AssociationThematic = typeof associationThematicValues[number];

export const associationThematicDefinitions: Record<
  AssociationThematic,
  { label: string; description: string }
> = {
  culture_loisirs: {
    label: "Culture & Loisirs",
    description: "Arts, sport, chasse, pêche, activités civiques et religieuses.",
  },
  social_sante: {
    label: "Social & Santé",
    description: "Caritatif, humanitaire, aide aux seniors, santé, services aux familles.",
  },
  education_formation: {
    label: "Éducation et formation",
    description: "Écoles, formation continue, apprentissage.",
  },
  economie_territoire: {
    label: "Économie & Territoire",
    description: "Emploi, insertion, logement, tourisme, défense d'intérêts économiques.",
  },
  environnement_patrimoine: {
    label: "Environnement et patrimoine",
    description: "Écologie, cadre de vie, protection des monuments.",
  },
  institutions_divers: {
    label: "Institutions & Divers",
    description: "Justice, sécurité civile, recherche, activités politiques.",
  },
};

export const associationThematicOptions = associationThematicValues.map((value) => ({
  value,
  label: associationThematicDefinitions[value].label,
  description: associationThematicDefinitions[value].description,
}));

export function getAssociationThematicLabel(value: AssociationThematic | string | null | undefined) {
  if (!value) return "Non renseignée";
  return associationThematicDefinitions[value as AssociationThematic]?.label ?? value;
}

export function getAssociationThematicDescription(value: AssociationThematic | string | null | undefined) {
  if (!value) return "";
  return associationThematicDefinitions[value as AssociationThematic]?.description ?? "";
}

export function parseAssociationThematics(value: string | null | undefined): AssociationThematic[] {
  if (!value) return [];
  try {
    const parsed = JSON.parse(value);
    if (Array.isArray(parsed)) {
      return parsed.filter((item): item is AssociationThematic =>
        associationThematicValues.includes(item as AssociationThematic)
      );
    }
  } catch {
    if (associationThematicValues.includes(value as AssociationThematic)) {
      return [value as AssociationThematic];
    }
  }
  return [];
}

export function serializeAssociationThematics(values: Array<AssociationThematic | string> | null | undefined) {
  const normalized = Array.from(
    new Set(
      (values || []).filter((item): item is AssociationThematic =>
        associationThematicValues.includes(item as AssociationThematic)
      )
    )
  );
  return normalized.length > 0 ? JSON.stringify(normalized) : null;
}

export function getAssociationThematicLabels(values: Array<AssociationThematic | string> | null | undefined) {
  return (values || []).map((value) => getAssociationThematicLabel(value));
}
