# CHANGE: Throught this file we're now using one `events` query to receive all related events
# (instead of separate queries for each event type)

# Some redundant fields (result of schema "flattening") were also removed from the events,
# as they can now be accessed though deep filtering.

# Note that in this case `orderBy` and `limit` now applies to all events together, not on per-type basis

query GetNotifications($memberId: String!, $limit: Int!) {
  notifications(
    limit: $limit
    orderBy: event_timestamp_DESC
    where: { member: { id_eq: $memberId } } # CHANGE: Simplified filtering
  ) {
    # CHANGE: The actual `event` is now nested as a property of `Notification`
    event {
      id
      timestamp # CHANGE: `timestamp` now used instead of `createdAt` (which is no longer available)
      inBlock
      data {
        ... on AuctionBidMadeEventData {
          bid {
            amount
            bidder {
              ...BasicMembershipFields
            }
            previousTopBid {
              bidder {
                ...BasicMembershipFields
              }
            }
            auction {
              nft {
                video {
                  id
                  title
                }
              }
            }
          }
          nftOwner {
            ...BasicNftOwnerFields
          }
        }

        ... on NftBoughtEventData {
          buyer {
            ...BasicMembershipFields
          }
          price
          nft {
            video {
              id
              title
            }
          }
        }

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

        ... on OpenAuctionBidAcceptedEventData {
          winningBid {
            amount
            bidder {
              ...BasicMembershipFields
            }
            auction {
              nft {
                video {
                  id
                  title
                }
              }
            }
          }
          previousNftOwner {
            ...BasicNftOwnerFields
          }
        }

        ... on EnglishAuctionSettledEventData {
          winningBid {
            bidder {
              ...BasicMembershipFields
            }
            auction {
              nft {
                video {
                  id
                  title
                }
              }
            }
          }
          previousNftOwner {
            ...BasicNftOwnerFields
          }
        }

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

# CHANGE: ID is now `String`
query GetNftHistory($nftId: String!) {
  nftHistoryEntries(
    orderBy: event_timestamp_DESC # CHANGE: `event_timestamp` now used instead of `createdAt` (which is no longer available)
    where: { nft: { id_eq: $nftId } } # CHANGE: Simplified filtering
  ) {
    # CHANGE: The actual `event` is now nested as a property of `NftHistoryEntry`
    event {
      id
      timestamp # CHANGE: `timestamp` now used instead of `createdAt` (which is no longer available)
      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 GetNftActivities($memberId: String!, $limit: Int!, $orderBy: [NftActivityOrderByInput!] = event_timestamp_DESC) {
  # COUNT queries
  nftsBought: nftActivitiesConnection(
    where: {
      event: {
        OR: [
          {
            data: {
              isTypeOf_in: [
                "EnglishAuctionSettledEventData"
                "BidMadeCompletingAuctionEventData"
                "OpenAuctionBidAcceptedEventData"
              ]
              winningBid: { bidder: { id_eq: $memberId } }
            }
          }
          { data: { isTypeOf_eq: "NftBoughtEventData", buyer: { id_eq: $memberId } } }
        ]
      }
    }
    orderBy: $orderBy
  ) {
    totalCount
  }

  nftsSold: nftActivitiesConnection(
    where: {
      event: {
        OR: [
          {
            data: {
              isTypeOf_in: [
                "EnglishAuctionSettledEventData"
                "BidMadeCompletingAuctionEventData"
                "OpenAuctionBidAcceptedEventData"
                "NftBoughtEventData"
              ]
              previousNftOwner: { member: { id_eq: $memberId } }
            }
          }
          {
            data: {
              isTypeOf_in: [
                "EnglishAuctionSettledEventData"
                "BidMadeCompletingAuctionEventData"
                "OpenAuctionBidAcceptedEventData"
                "NftBoughtEventData"
              ]
              previousNftOwner: { channel: { ownerMember: { id_eq: $memberId } } }
            }
          }
        ]
      }
    }
    orderBy: $orderBy
  ) {
    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: $orderBy
  ) {
    totalCount
  }

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

  # DATA query
  nftActivities(
    limit: $limit
    orderBy: $orderBy # CHANGE: `event_timestamp` now used instead of `createdAt` (which is no longer available)
    where: { member: { id_eq: $memberId } } # CHANGE: Simplified filtering
  ) {
    # CHANGE: The actual `event` is now nested as a property of `NftActivity`
    event {
      id
      timestamp # CHANGE: `timestamp` now used instead of `createdAt` (which is no longer available)
      inBlock
      data {
        ... on AuctionBidMadeEventData {
          nftOwner {
            ...BasicNftOwnerFields
          }
          bid {
            amount
            bidder {
              ...BasicMembershipFields
            }
            previousTopBid {
              bidder {
                ...BasicMembershipFields
              }
            }
            auction {
              nft {
                video {
                  ...BasicVideoActivityFields
                }
              }
            }
          }
        }
        ... on EnglishAuctionSettledEventData {
          previousNftOwner {
            ...BasicNftOwnerFields
          }
          winningBid {
            bidder {
              ...BasicMembershipFields
            }
            amount
            auction {
              nft {
                video {
                  ...BasicVideoActivityFields
                }
              }
            }
          }
        }
        ... on NftBoughtEventData {
          buyer {
            ...BasicMembershipFields
          }
          previousNftOwner {
            ...BasicNftOwnerFields
          }
          nft {
            video {
              ...BasicVideoActivityFields
            }
          }
          price
        }
        ... on BidMadeCompletingAuctionEventData {
          previousNftOwner {
            ...BasicNftOwnerFields
          }
          winningBid {
            bidder {
              ...BasicMembershipFields
            }
            auction {
              nft {
                video {
                  ...BasicVideoActivityFields
                }
              }
            }
            amount
          }
        }
        ... on OpenAuctionBidAcceptedEventData {
          winningBid {
            amount
            bidder {
              ...BasicMembershipFields
            }
            auction {
              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 {
            auction {
              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
          }
        }
      }
    }
  }
}
