import {z} from 'zod'

export const CoordinateSchema = z.object({
  latitude: z.number().min(-90).max(90),
  longitude: z.number().min(-180).max(180),
})

// a geocoder result; passed back to the api as-is when picking a place
export const PlaceRefSchema = z.object({
  id: z.string().max(200),
  provider: z.literal('locationiq'),
  osmType: z.string().max(16).optional(),
  osmId: z.string().max(32).optional(),
  name: z.string().trim().min(1).max(200),
  category: z.string().max(80).optional(),
  address: z.string().max(500).optional(),
  coordinate: CoordinateSchema,
})

export const PlaceStatusSchema = z.enum([
  'canonical',
  'provisional',
  'merged',
])

// a canonical place entity as embedded in crumb responses
export const CrumbPlaceSchema = z.object({
  id: z.uuid(),
  name: z.string(),
  latitude: z.number().nullable(),
  longitude: z.number().nullable(),
  address: z.string().nullable(),
  category: z.string().nullable(),
  status: PlaceStatusSchema,
})

export const SearchNearbyOptionsSchema = z.object({
  radiusMeters: z.number().int().positive().optional(),
  limit: z.number().int().positive().optional(),
})

export type Coordinate = z.infer<typeof CoordinateSchema>
export type PlaceRef = z.infer<typeof PlaceRefSchema>
export type PlaceStatus = z.infer<typeof PlaceStatusSchema>
export type CrumbPlace = z.infer<typeof CrumbPlaceSchema>
export type SearchNearbyOptions = z.infer<
  typeof SearchNearbyOptionsSchema
>
