
mutation createUserConversation(
  $conversationId: UUID!
  $userId: String!
  $unreadCount: Int
  $lastReadAt: Timestamp
) @auth(level: USER) {
  userConversation_insert(
    data: {
      conversationId: $conversationId
      userId: $userId
      unreadCount: $unreadCount
      lastReadAt: $lastReadAt
    }
  )
}

mutation updateUserConversation(
  $conversationId: UUID!
  $userId: String!
  $unreadCount: Int
  $lastReadAt: Timestamp
) @auth(level: USER) {
  userConversation_update(
    key: { conversationId: $conversationId, userId: $userId }
    data: {
      unreadCount: $unreadCount
      lastReadAt: $lastReadAt
    }
  )
}

# ----------------------------------------------------------
# MARK AS READ (sets unreadCount=0 + lastReadAt=now)
# Note: request.time is valid in schema defaults; in mutations
# you can pass Timestamp from client. If you want "now" server-side,
# you'd typically pass null and have logic elsewhere.
# ----------------------------------------------------------
mutation markConversationAsRead(
  $conversationId: UUID!
  $userId: String!
  $lastReadAt: Timestamp
) @auth(level: USER) {
  userConversation_update(
    key: { conversationId: $conversationId, userId: $userId }
    data: {
      unreadCount: 0
      lastReadAt: $lastReadAt
    }
  )
}

# ----------------------------------------------------------
# INCREMENT UNREAD (common after sending message)
# ----------------------------------------------------------
mutation incrementUnreadForUser(
  $conversationId: UUID!
  $userId: String!
  $unreadCount: Int!
) @auth(level: USER) {
  userConversation_update(
    key: { conversationId: $conversationId, userId: $userId }
    data: { unreadCount: $unreadCount }
  )
}

mutation deleteUserConversation(
  $conversationId: UUID!
  $userId: String!
) @auth(level: USER) {
  userConversation_delete(
    key: { conversationId: $conversationId, userId: $userId }
  )
}
