import type { Key } from "react-aria-components"

import type { JobylonFilters } from "@/types/components/blocks/jobylonFeed"
import { JobylonFilterKey } from "@/types/enums/jobylonFeed"
import type { JobylonItem } from "@/types/trpc/routers/jobylon"

export function getFiltersFromJobs(jobs: JobylonItem[]) {
  const filters = jobs.reduce<JobylonFilters>(
    (acc, job) => {
      if (job.locations.length) {
        job.locations.forEach((location) => {
          if (location.country && location.countryShort) {
            if (
              !acc[JobylonFilterKey.country].find(
                (f) => f.value === location.countryShort
              )
            ) {
              acc[JobylonFilterKey.country].push({
                value: location.countryShort,
                label: location.country,
              })
            }
          }
          if (location.city) {
            if (
              !acc[JobylonFilterKey.city].find((f) => f.value === location.city)
            ) {
              acc[JobylonFilterKey.city].push({
                value: location.city,
                label: location.city,
              })
            }
          }
        })
      }
      if (job.departments.length) {
        job.departments.forEach((department) => {
          if (
            !acc[JobylonFilterKey.department].find(
              (f) => f.value === department.id
            )
          ) {
            acc[JobylonFilterKey.department].push({
              value: department.id,
              label: department.name,
            })
          }
        })
      }
      if (job.categories.length) {
        job.categories.forEach((category) => {
          if (
            !acc[JobylonFilterKey.category].find(
              (f) => f.value === category.text
            )
          ) {
            acc[JobylonFilterKey.category].push({
              value: category.text,
              label: category.text,
            })
          }
        })
      }
      return acc
    },
    {
      [JobylonFilterKey.country]: [],
      [JobylonFilterKey.category]: [],
      [JobylonFilterKey.city]: [],
      [JobylonFilterKey.department]: [],
    }
  )

  // Sort each filter array by label
  Object.keys(filters).forEach((key) => {
    const typedKey = key as JobylonFilterKey
    filters[typedKey].sort((a, b) => a.label.localeCompare(b.label))
  })

  return filters
}

export function getFilteredJobs(
  jobs: JobylonItem[],
  filters: Record<JobylonFilterKey, Key>
) {
  const countryFilter = filters[JobylonFilterKey.country]
  const cityFilter = filters[JobylonFilterKey.city]
  const categoryFilter = filters[JobylonFilterKey.category]
  const departmentFilter = filters[JobylonFilterKey.department]

  return jobs.filter((job) => {
    const countryMatch = countryFilter
      ? job.locations.some(
          (location) => location.countryShort === countryFilter
        )
      : true
    const cityMatch = cityFilter
      ? job.locations.some((location) => location.city === cityFilter)
      : true
    const departmentMatch = filters[JobylonFilterKey.department]
      ? job.departments.some((department) => department.id === departmentFilter)
      : true
    const categoryMatch = filters[JobylonFilterKey.category]
      ? job.categories.some((category) => category.text === categoryFilter)
      : true
    return countryMatch && cityMatch && departmentMatch && categoryMatch
  })
}
