import {z} from 'zod'

export const HandleSchema = z
  .string()
  .trim()
  .toLowerCase()
  .min(3)
  .max(30)
  .regex(
    /^[a-z0-9]+(-[a-z0-9]+)*$/,
    'handle must be lowercase letters, numbers and single dashes',
  )

export const FriendshipStatusSchema = z.enum([
  'self',
  'none',
  'pending_outgoing',
  'pending_incoming',
  'friends',
  // the viewer has blocked this user; only ever set on a direct profile
  // view (blocked users never surface in lists), so the ui can offer unblock
  'blocked',
])

export const PublicProfileSchema = z.object({
  id: z.uuid(),
  handle: z.string(),
  avatarUrl: z.url().nullable(),
  createdAt: z.iso.datetime(),
  friendCount: z.number().int().nonnegative(),
  friendshipStatus: FriendshipStatusSchema,
})

// profile header: counts + a single preview each; the crumbs/trails tabs
// load their own paginated lists rather than inlining whole arrays
export const ProfileDetailSchema = PublicProfileSchema.extend({
  crumbCount: z.number().int().nonnegative(),
  trailCount: z.number().int().nonnegative(),
  crumbPreview: z.string().nullable(),
  trailPreview: z.string().nullable(),
})

export const UpdateHandleSchema = z.object({handle: HandleSchema})

export const SendFriendRequestSchema = z.object({
  addresseeId: z.uuid(),
})

export const FriendRequestSchema = z.object({
  id: z.uuid(),
  requester: PublicProfileSchema,
  createdAt: z.iso.datetime(),
})

export type FriendshipStatus = z.infer<typeof FriendshipStatusSchema>
export type PublicProfile = z.infer<typeof PublicProfileSchema>
export type ProfileDetail = z.infer<typeof ProfileDetailSchema>
export type FriendRequest = z.infer<typeof FriendRequestSchema>
