import { z } from "zod"

import { nullableNumberValidator } from "@/utils/zod/numberValidator"
import { nullableStringValidator } from "@/utils/zod/stringValidator"

import { getPoiGroupByCategoryName } from "../../utils"
import { locationSchema } from "./location"

export const pointOfInterestSchema = z
  .object({
    category: z.object({
      name: nullableStringValidator,
    }),
    distance: nullableNumberValidator,
    location: locationSchema,
    name: nullableStringValidator,
  })
  .transform((poi) => ({
    categoryName: poi.category.name,
    coordinates: {
      lat: poi.location.latitude,
      lng: poi.location.longitude,
    },
    distance: poi.distance,
    group: getPoiGroupByCategoryName(poi.category.name),
    name: poi.name,
  }))

export const pointOfInterestsSchema = z
  .array(pointOfInterestSchema)
  .nullish()
  .transform((arr) => (arr ? arr.filter(Boolean) : []))
  .transform((pois) =>
    pois.sort((a, b) => (a.distance ?? 0) - (b.distance ?? 0))
  )
