import { SKILL_LEVEL_OPTIONS } from '../constants/skillLevels'

const LEVEL_FILTER_OPTS = SKILL_LEVEL_OPTIONS.filter((o) => o.level != null)

function pushCatalogRuleFilterChips(chips, field, rules, options, topicLabel, setFilters) {
  ;(rules || []).forEach((r) => {
    const rid = String(r.id ?? r.focus_area_id ?? '')
    const opt = options.find((o) => String(o.id) === rid)
    chips.push({
      key: `${field}-${r.key}`,
      label: `${topicLabel}: ${r.mode === 'forbid' ? '−' : '+'} ${opt?.label ?? rid}`,
      onRemove: () =>
        setFilters((prev) => ({
          ...prev,
          [field]: (prev[field] || []).filter((x) => x.key !== r.key),
        })),
    })
  })
}

function levelOptionShort(levelStr) {
  const o = LEVEL_FILTER_OPTS.find((x) => String(x.level) === String(levelStr))
  return o ? String(o.level) : String(levelStr)
}

export function buildExerciseListFilterChips({
  mineOnly,
  setMineOnly,
  filters,
  setFilters,
  focusOptions,
  styleOptions,
  trainingTypeOptions,
  targetGroupOptions,
  skillOptions,
  visibilityOptions,
  statusOptions,
}) {
  const chips = []

  if (mineOnly) {
    chips.push({
      key: 'mine-only',
      label: 'Nur von mir erstellt',
      onRemove: () => setMineOnly(false),
    })
  }

  pushCatalogRuleFilterChips(chips, 'focus_rules', filters.focus_rules, focusOptions, 'Fokus', setFilters)

  if (filters.focus_only_without) {
    chips.push({
      key: 'focus-only-none',
      label: 'Nur ohne Fokusbereich',
      onRemove: () => setFilters((prev) => ({ ...prev, focus_only_without: false })),
    })
  }

  ;(filters.focus_area_ids || []).forEach((id) => {
    const opt = focusOptions.find((o) => String(o.id) === String(id))
    chips.push({
      key: `fa-${id}`,
      label: `Fokus (ODER, älter): ${opt?.label ?? id}`,
      onRemove: () =>
        setFilters((prev) => ({
          ...prev,
          focus_area_ids: prev.focus_area_ids.filter((x) => String(x) !== String(id)),
        })),
    })
  })

  pushCatalogRuleFilterChips(
    chips,
    'style_direction_rules',
    filters.style_direction_rules,
    styleOptions,
    'Stil',
    setFilters
  )
  pushCatalogRuleFilterChips(
    chips,
    'training_type_rules',
    filters.training_type_rules,
    trainingTypeOptions,
    'Trainingsstil',
    setFilters
  )
  pushCatalogRuleFilterChips(
    chips,
    'target_group_rules',
    filters.target_group_rules,
    targetGroupOptions,
    'Zielgruppe',
    setFilters
  )

  ;(filters.style_direction_ids || []).forEach((id) => {
    const opt = styleOptions.find((o) => String(o.id) === String(id))
    chips.push({
      key: `sd-${id}`,
      label: `Stil (ODER, älter): ${opt?.label ?? id}`,
      onRemove: () =>
        setFilters((prev) => ({
          ...prev,
          style_direction_ids: prev.style_direction_ids.filter((x) => String(x) !== String(id)),
        })),
    })
  })
  ;(filters.training_type_ids || []).forEach((id) => {
    const opt = trainingTypeOptions.find((o) => String(o.id) === String(id))
    chips.push({
      key: `tt-${id}`,
      label: `Trainingsstil (ODER, älter): ${opt?.label ?? id}`,
      onRemove: () =>
        setFilters((prev) => ({
          ...prev,
          training_type_ids: prev.training_type_ids.filter((x) => String(x) !== String(id)),
        })),
    })
  })
  ;(filters.target_group_ids || []).forEach((id) => {
    const opt = targetGroupOptions.find((o) => String(o.id) === String(id))
    chips.push({
      key: `tg-${id}`,
      label: `Zielgruppe (ODER, älter): ${opt?.label ?? id}`,
      onRemove: () =>
        setFilters((prev) => ({
          ...prev,
          target_group_ids: prev.target_group_ids.filter((x) => String(x) !== String(id)),
        })),
    })
  })

  ;(filters.skill_ids || []).forEach((id) => {
    const opt = skillOptions.find((o) => String(o.id) === String(id))
    chips.push({
      key: `sk-${id}`,
      label: `Fähigkeit: ${opt?.label ?? id}`,
      onRemove: () =>
        setFilters((prev) => ({
          ...prev,
          skill_ids: prev.skill_ids.filter((x) => String(x) !== String(id)),
        })),
    })
  })

  if (filters.skill_min_level || filters.skill_max_level) {
    const a = filters.skill_min_level ? levelOptionShort(filters.skill_min_level) : '…'
    const b = filters.skill_max_level ? levelOptionShort(filters.skill_max_level) : '…'
    chips.push({
      key: 'skill-levels',
      label: `Stufe ${a}–${b}`,
      onRemove: () =>
        setFilters((prev) => ({
          ...prev,
          skill_min_level: '',
          skill_max_level: '',
        })),
    })
  }

  pushCatalogRuleFilterChips(
    chips,
    'visibility_rules',
    filters.visibility_rules,
    visibilityOptions,
    'Sichtbarkeit',
    setFilters
  )
  pushCatalogRuleFilterChips(chips, 'status_rules', filters.status_rules, statusOptions, 'Status', setFilters)

  if (filters.exclude_without_focus) {
    chips.push({
      key: 'ex-no-focus',
      label: 'Ohne Fokus ausblenden',
      onRemove: () => setFilters((prev) => ({ ...prev, exclude_without_focus: false })),
    })
  }
  if (filters.include_archived) {
    chips.push({
      key: 'inc-arch',
      label: 'Archivierte anzeigen',
      onRemove: () => setFilters((prev) => ({ ...prev, include_archived: false })),
    })
  }

  return chips
}
