
# ----------------------------------------------------------
# LIST ALL (admin/debug)
# ----------------------------------------------------------
query listUserConversations(
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  userConversations(offset: $offset, limit: $limit) {
    id
    conversationId
    userId
    unreadCount
    lastReadAt
    createdAt

    conversation {
      id
      subject
      status
      conversationType
      isGroup
      groupName
      lastMessage
      lastMessageAt
      createdAt
    }

    user {
      id
      fullName
      photoUrl
    }
  }
}

# ----------------------------------------------------------
# GET BY KEY (conversationId + userId)
# ----------------------------------------------------------
query getUserConversationByKey(
  $conversationId: UUID!
  $userId: String!
) @auth(level: USER) {
  userConversation(key: { conversationId: $conversationId, userId: $userId }) {
    id
    conversationId
    userId
    unreadCount
    lastReadAt
    createdAt

    conversation {
      id
      subject
      status
      conversationType
      isGroup
      groupName
      lastMessage
      lastMessageAt
      createdAt
    }

    user {
      id
      fullName
      photoUrl
    }
  }
}

# ----------------------------------------------------------
# LIST BY USER (My inbox) ✅
# Order by most recent activity (Conversation.lastMessageAt)
# NOTE: If your DC version doesn't allow orderBy on nested fields,
# just orderBy createdAt/updatedAt on user_conversations.
# ----------------------------------------------------------
query listUserConversationsByUserId(
  $userId: String!
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  userConversations(
    where: { userId: { eq: $userId } }
    offset: $offset
    limit: $limit
    # If supported:
    # orderBy: { conversation: { lastMessageAt: DESC } }
    orderBy: { updatedAt: DESC }
  ) {
    id
    conversationId
    userId
    unreadCount
    lastReadAt
    createdAt
    updatedAt
    createdBy

    conversation {
      id
      subject
      status
      conversationType
      isGroup
      groupName
      lastMessage
      lastMessageAt
      createdAt
    }

    user {
      id
      fullName
      photoUrl
    }
  }
}

# ----------------------------------------------------------
# LIST UNREAD BY USER (badge, notifications)
# ----------------------------------------------------------
query listUnreadUserConversationsByUserId(
  $userId: String!
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  userConversations(
    where: {
      userId: { eq: $userId }
      unreadCount: { gt: 0 }
    }
    offset: $offset
    limit: $limit
    orderBy: { updatedAt: DESC }
  ) {
    id
    conversationId
    userId
    unreadCount
    lastReadAt
    createdAt

    conversation {
      id
      subject
      status
      conversationType
      isGroup
      groupName
      lastMessage
      lastMessageAt
    }
  }
}

# ----------------------------------------------------------
# LIST BY CONVERSATION (participants view)
# ----------------------------------------------------------
query listUserConversationsByConversationId(
  $conversationId: UUID!
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  userConversations(
    where: { conversationId: { eq: $conversationId } }
    offset: $offset
    limit: $limit
    orderBy: { createdAt: ASC }
  ) {
    id
    conversationId
    userId
    unreadCount
    lastReadAt
    createdAt

    user {
      id
      fullName
      photoUrl
    }
  }
}

# ----------------------------------------------------------
# FILTER (dashboard/debug)
# ----------------------------------------------------------
query filterUserConversations(
  $userId: String
  $conversationId: UUID
  $unreadMin: Int
  $unreadMax: Int
  $lastReadAfter: Timestamp
  $lastReadBefore: Timestamp
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  userConversations(
    where: {
      userId: { eq: $userId }
      conversationId: { eq: $conversationId }
      unreadCount: { ge: $unreadMin, le: $unreadMax }
      lastReadAt: { ge: $lastReadAfter, le: $lastReadBefore }
    }
    offset: $offset
    limit: $limit
    orderBy: { updatedAt: DESC }
  ) {
    id
    conversationId
    userId
    unreadCount
    lastReadAt
    createdAt

    conversation {
      id
      subject
      status
      conversationType
      isGroup
      groupName
      lastMessage
      lastMessageAt
      createdAt
    }
    user { id fullName photoUrl }
  }
}
