import {z} from 'zod'

import {PublicProfileSchema} from './friends'
import {
  ReactionEmojiSchema,
  TrailPostSchema,
  TrailWithCrumbsSchema,
} from './trails'

export const TrailViewerRoleSchema = z.enum([
  'owner',
  'collaborator',
  'viewer',
])

export const TrailDetailSchema = TrailWithCrumbsSchema.extend({
  collaborators: z.array(PublicProfileSchema),
  viewerRole: TrailViewerRoleSchema,
  posts: z.array(TrailPostSchema),
})

export const TrailInviteSchema = z.object({
  id: z.uuid(),
  trail: z.object({
    id: z.uuid(),
    name: z.string(),
    description: z.string().nullable(),
    coverImageUrl: z.url().nullable(),
    crumbCount: z.number().int().nonnegative(),
    memberCount: z.number().int().nonnegative(),
  }),
  inviter: PublicProfileSchema,
  createdAt: z.iso.datetime(),
})

export const SendTrailInviteSchema = z.object({
  inviteeId: z.uuid(),
})

// one person who reacted to a crumb in a trail, with the single emoji they
// picked (reactions are one-per-user-per-crumb) and their public profile
export const CrumbReactorSchema = z.object({
  user: PublicProfileSchema,
  emoji: ReactionEmojiSchema,
})

export const CrumbReactorsResponseSchema = z.object({
  reactors: z.array(CrumbReactorSchema),
})

export type TrailViewerRole = z.infer<typeof TrailViewerRoleSchema>
export type TrailDetail = z.infer<typeof TrailDetailSchema>
export type TrailInvite = z.infer<typeof TrailInviteSchema>
export type CrumbReactor = z.infer<typeof CrumbReactorSchema>
