query GetNotificationsConnection($memberId: String!, $first: Int!, $after: String) {
  notificationsConnection(
    first: $first
    after: $after
    orderBy: event_timestamp_DESC
    where: { member: { id_eq: $memberId } }
  ) {
    pageInfo {
      hasNextPage
      endCursor
    }
    totalCount
    edges {
      cursor
      node {
        event {
          id
          timestamp
          inBlock
          data {
            ... on AuctionBidMadeEventData {
              bid {
                amount
                bidder {
                  ...BasicMembershipFields
                }
                previousTopBid {
                  bidder {
                    ...BasicMembershipFields
                  }
                }
                nft {
                  video {
                    ...BasicVideoActivityFields
                  }
                }
              }
              nftOwner {
                ...BasicNftOwnerFields
              }
            }

            ... on NftBoughtEventData {
              buyer {
                ...BasicMembershipFields
              }
              price
              nft {
                video {
                  ...BasicVideoActivityFields
                }
              }
            }

            ... on BidMadeCompletingAuctionEventData {
              winningBid {
                bidder {
                  ...BasicMembershipFields
                }
                amount
                nft {
                  video {
                    ...BasicVideoActivityFields
                  }
                }
              }
              previousNftOwner {
                ...BasicNftOwnerFields
              }
            }

            ... on OpenAuctionBidAcceptedEventData {
              winningBid {
                amount
                bidder {
                  ...BasicMembershipFields
                }
                nft {
                  video {
                    ...BasicVideoActivityFields
                  }
                }
              }
              previousNftOwner {
                ...BasicNftOwnerFields
              }
            }

            ... on EnglishAuctionSettledEventData {
              winningBid {
                bidder {
                  ...BasicMembershipFields
                }
                nft {
                  video {
                    ...BasicVideoActivityFields
                  }
                }
              }
              previousNftOwner {
                ...BasicNftOwnerFields
              }
            }

            ... on CommentCreatedEventData {
              comment {
                id
                video {
                  id
                  title
                }
                parentComment {
                  id
                }
                author {
                  ...BasicMembershipFields
                }
              }
            }
          }
        }
      }
    }
  }
}

query GetNftHistory($nftId: String!) {
  nftHistoryEntries(orderBy: event_timestamp_DESC, where: { nft: { id_eq: $nftId } }) {
    event {
      id
      timestamp
      data {
        ... on NftIssuedEventData {
          nftOwner {
            ...BasicNftOwnerFields
          }
        }

        ... on OpenAuctionStartedEventData {
          nftOwner {
            ...BasicNftOwnerFields
          }
        }

        ... on EnglishAuctionStartedEventData {
          nftOwner {
            ...BasicNftOwnerFields
          }
        }

        ... on NftSellOrderMadeEventData {
          price
          nftOwner {
            ...BasicNftOwnerFields
          }
        }

        ... on AuctionBidMadeEventData {
          bid {
            bidder {
              ...BasicMembershipFields
            }
            amount
          }
        }

        ... on BidMadeCompletingAuctionEventData {
          previousNftOwner {
            ...BasicNftOwnerFields
          }
          winningBid {
            bidder {
              ...BasicMembershipFields
            }
            amount
          }
        }

        ... on NftBoughtEventData {
          buyer {
            ...BasicMembershipFields
          }
          price
        }

        ... on EnglishAuctionSettledEventData {
          previousNftOwner {
            ...BasicNftOwnerFields
          }
          winningBid {
            bidder {
              ...BasicMembershipFields
            }
          }
        }

        ... on OpenAuctionBidAcceptedEventData {
          previousNftOwner {
            ...BasicNftOwnerFields
          }
          winningBid {
            amount
            bidder {
              ...BasicMembershipFields
            }
          }
        }

        ... on AuctionBidCanceledEventData {
          member {
            ...BasicMembershipFields
          }
        }

        ... on AuctionCanceledEventData {
          nftOwner {
            ...BasicNftOwnerFields
          }
        }

        ... on BuyNowCanceledEventData {
          nftOwner {
            ...BasicNftOwnerFields
          }
        }

        ... on BuyNowPriceUpdatedEventData {
          nftOwner {
            ...BasicNftOwnerFields
          }
          newPrice
        }
      }
    }
  }
}

query GetNftActivitiesCount($memberId: String!) {
  nftsBought: nftActivitiesConnection(
    where: {
      OR: [
        {
          member: { id_eq: $memberId }
          event: {
            data: {
              isTypeOf_in: [
                "EnglishAuctionSettledEventData"
                "BidMadeCompletingAuctionEventData"
                "OpenAuctionBidAcceptedEventData"
              ]
              winningBid: { bidder: { id_eq: $memberId } }
            }
          }
        }
        {
          member: { id_eq: $memberId }
          event: { data: { isTypeOf_eq: "NftBoughtEventData", buyer: { id_eq: $memberId } } }
        }
      ]
    }
    orderBy: event_timestamp_DESC
  ) {
    totalCount
  }

  nftsSold: nftActivitiesConnection(
    where: {
      OR: [
        {
          member: { id_eq: $memberId }
          event: {
            data: {
              isTypeOf_in: [
                "EnglishAuctionSettledEventData"
                "BidMadeCompletingAuctionEventData"
                "OpenAuctionBidAcceptedEventData"
                "NftBoughtEventData"
              ]
              previousNftOwner: { member: { id_eq: $memberId } }
            }
          }
        }
        {
          member: { id_eq: $memberId }
          event: {
            data: {
              isTypeOf_in: [
                "EnglishAuctionSettledEventData"
                "BidMadeCompletingAuctionEventData"
                "OpenAuctionBidAcceptedEventData"
                "NftBoughtEventData"
              ]
              previousNftOwner: { channel: { ownerMember: { id_eq: $memberId } } }
            }
          }
        }
      ]
    }
    orderBy: event_timestamp_DESC
  ) {
    totalCount
  }

  nftsIssued: nftActivitiesConnection(
    where: {
      event: {
        OR: [
          { data: { isTypeOf_eq: "NftIssuedEventData", nftOwner: { channel: { ownerMember: { id_eq: $memberId } } } } }
          { data: { isTypeOf_eq: "NftIssuedEventData", nftOwner: { member: { id_eq: $memberId } } } }
        ]
      }
    }
    orderBy: event_timestamp_DESC
  ) {
    totalCount
  }

  nftsBidded: nftActivitiesConnection(
    where: { event: { data: { isTypeOf_eq: "AuctionBidMadeEventData", bid: { bidder: { id_eq: $memberId } } } } }
    orderBy: event_timestamp_DESC
  ) {
    totalCount
  }
}

query GetNftActivities(
  $memberId: String!
  $first: Int!
  $after: String
  $orderBy: [NftActivityOrderByInput!] = event_timestamp_DESC
) {
  nftActivitiesConnection(first: $first, after: $after, orderBy: $orderBy, where: { member: { id_eq: $memberId } }) {
    totalCount
    pageInfo {
      endCursor
      hasNextPage
    }
    edges {
      cursor
      node {
        event {
          id
          timestamp
          inBlock
          data {
            ... on AuctionBidMadeEventData {
              nftOwner {
                ...BasicNftOwnerFields
              }
              bid {
                amount
                bidder {
                  ...BasicMembershipFields
                }
                previousTopBid {
                  bidder {
                    ...BasicMembershipFields
                  }
                }
                nft {
                  video {
                    ...BasicVideoActivityFields
                  }
                }
              }
            }
            ... on EnglishAuctionSettledEventData {
              previousNftOwner {
                ...BasicNftOwnerFields
              }
              winningBid {
                bidder {
                  ...BasicMembershipFields
                }
                amount
                nft {
                  video {
                    ...BasicVideoActivityFields
                  }
                }
              }
            }
            ... on NftBoughtEventData {
              buyer {
                ...BasicMembershipFields
              }
              previousNftOwner {
                ...BasicNftOwnerFields
              }
              nft {
                video {
                  ...BasicVideoActivityFields
                }
              }
              price
            }
            ... on BidMadeCompletingAuctionEventData {
              previousNftOwner {
                ...BasicNftOwnerFields
              }
              winningBid {
                bidder {
                  ...BasicMembershipFields
                }

                nft {
                  video {
                    ...BasicVideoActivityFields
                  }
                }
                amount
              }
            }
            ... on OpenAuctionBidAcceptedEventData {
              winningBid {
                amount
                bidder {
                  ...BasicMembershipFields
                }
                nft {
                  video {
                    ...BasicVideoActivityFields
                  }
                }
              }
              previousNftOwner {
                ...BasicNftOwnerFields
              }
            }
            ... on EnglishAuctionStartedEventData {
              auction {
                nft {
                  video {
                    ...BasicVideoActivityFields
                  }
                }
              }
              nftOwner {
                ...BasicNftOwnerFields
              }
            }
            ... on OpenAuctionStartedEventData {
              auction {
                nft {
                  video {
                    ...BasicVideoActivityFields
                  }
                }
              }
              nftOwner {
                ...BasicNftOwnerFields
              }
            }
            ... on NftSellOrderMadeEventData {
              price
              nft {
                video {
                  ...BasicVideoActivityFields
                }
              }
              nftOwner {
                ...BasicNftOwnerFields
              }
            }
            ... on AuctionBidCanceledEventData {
              member {
                ...BasicMembershipFields
              }
              bid {
                nft {
                  video {
                    ...BasicVideoActivityFields
                  }
                }
              }
            }
            ... on BuyNowCanceledEventData {
              nft {
                video {
                  ...BasicVideoActivityFields
                }
              }
              nftOwner {
                ...BasicNftOwnerFields
              }
            }
            ... on AuctionCanceledEventData {
              auction {
                nft {
                  video {
                    ...BasicVideoActivityFields
                  }
                }
              }
              nftOwner {
                ...BasicNftOwnerFields
              }
            }
            ... on BuyNowPriceUpdatedEventData {
              newPrice
              nft {
                video {
                  ...BasicVideoActivityFields
                }
              }
              nftOwner {
                ...BasicNftOwnerFields
              }
            }
            ... on NftIssuedEventData {
              nft {
                video {
                  ...BasicVideoActivityFields
                }
              }
              nftOwner {
                ...BasicNftOwnerFields
              }
            }
          }
        }
      }
    }
  }
}
