# CHANGE: Dropped because unused:
# query GetBasicChannel(...) { ... }

query GetFullChannel($id: String!) {
  # CHANGE: `channelByUniqueInput` is no longer supported in subsquid, replaced with `channelById`
  channelById(id: $id) {
    ...FullChannelFields
  }
}

# CHANGE: `channels` replaced with `extendedChannels` (which include `activeVideosCount`)
query GetExtendedBasicChannels(
  $where: ExtendedChannelWhereInput
  $limit: Int = 50
  $orderBy: [ChannelOrderByInput!] = [createdAt_DESC]
) {
  extendedChannels(where: $where, orderBy: $orderBy, limit: $limit) {
    ...ExtendedBasicChannelFields
  }
}

# CHANGE: `channels` replaced with `extendedChannels` (which include `activeVideosCount`)
query GetExtendedFullChannels(
  $where: ExtendedChannelWhereInput
  $limit: Int = 50
  $orderBy: [ChannelOrderByInput!] = [createdAt_DESC]
) {
  extendedChannels(where: $where, orderBy: $orderBy, limit: $limit) {
    ...ExtendedFullChannelFields
  }
}

# No changes (UNLESS `activeVideosCount` is needed in "where")
query GetBasicChannelsConnection(
  $first: Int
  $after: String
  $where: ChannelWhereInput
  $orderBy: [ChannelOrderByInput!] = [createdAt_DESC]
) {
  channelsConnection(first: $first, after: $after, where: $where, orderBy: $orderBy) {
    edges {
      cursor
      node {
        ...BasicChannelFields
      }
    }
    pageInfo {
      hasNextPage
      endCursor
    }
    totalCount
  }
}

# CHANGE: `ID` is now `String`
mutation FollowChannel($channelId: String!) {
  followChannel(channelId: $channelId) {
    channelId
    follows
    cancelToken
  }
}

# CHANGE: `ID` is now `String`
mutation UnfollowChannel($channelId: String!, $token: String!) {
  unfollowChannel(channelId: $channelId, token: $token) {
    channelId
    follows
  }
}

# CHANGE: Removed because unused:
# query GetMostViewedChannelsConnection(...) { ... }

query GetTop10Channels($where: ExtendedChannelWhereInput) {
  # CHANGE: Replacement for overly-specific `top10Channels` query
  extendedChannels(where: $where, orderBy: followsNum_DESC, limit: 10) {
    channel {
      ...BasicChannelFields
    }
  }
}

query GetDiscoverChannels($where: ExtendedChannelWhereInput) {
  # CHANGE: Replacement for overly-specific `discoverChannels` query
  mostRecentChannels(where: $where, orderBy: followsNum_DESC, mostRecentLimit: 100, resultsLimit: 15) {
    channel {
      ...BasicChannelFields
    }
  }
}

# CHANGE: Replaced with a custom `channelNftCollectors` query taking `channelId` argument (instead of `ChannelNftCollectorsWhereInput`)
query GetChannelNftCollectors($channelId: String!, $orderBy: ChannelNftCollectorsOrderByInput = amount_DESC) {
  channelNftCollectors(channelId: $channelId, orderBy: $orderBy) {
    member {
      ...BasicMembershipFields
    }
    amount
  }
}

query GetTotalChannelsAndTotalVideos($memberId: String!, $channelId: String!) {
  membershipById(id: $memberId) {
    totalChannelsCreated
    channels(where: { id_eq: $channelId }) {
      totalVideosCreated
    }
  }
}

# CHANGE: `ID` is now `String`
mutation ReportChannel($channelId: String!, $rationale: String!) {
  reportChannel(channelId: $channelId, rationale: $rationale) {
    id
    channelId
  }
}

query GetPayloadDataByCommitment($commitment: String!) {
  events(where: { data: { isTypeOf_eq: "ChannelPayoutsUpdatedEventData", commitment_eq: $commitment } }) {
    data {
      ... on ChannelPayoutsUpdatedEventData {
        payloadDataObject {
          ...StorageDataObjectFields
        }
      }
    }
  }
}

query GetChannelPaymentEvents($channelId: String) {
  events(
    orderBy: timestamp_DESC
    where: {
      OR: [
        # This covers sold events
        {
          data: {
            isTypeOf_in: [
              "NftBoughtEventData"
              "BidMadeCompletingAuctionEventData"
              "EnglishAuctionSettledEventData"
              "OpenAuctionBidAcceptedEventData"
            ]
            previousNftOwner: { channel: { id_eq: $channelId } }
          }
        }
        # This covers royalty events
        {
          data: {
            isTypeOf_in: [
              "NftBoughtEventData"
              "BidMadeCompletingAuctionEventData"
              "EnglishAuctionSettledEventData"
              "OpenAuctionBidAcceptedEventData"
            ]
            nft: { video: { channel: { id_eq: $channelId } } }
          }
        }
        {
          data: {
            isTypeOf_in: ["ChannelRewardClaimedEventData", "ChannelFundsWithdrawnEventData"]
            channel: { id_eq: $channelId }
          }
        }
        { data: { isTypeOf_in: ["ChannelPaymentMadeEventData"], payeeChannel: { id_eq: $channelId } } }
      ]
    }
  ) {
    inBlock
    timestamp
    data {
      __typename
      ... on NftBoughtEventData {
        price
        buyer {
          controllerAccount
        }
        nft {
          creatorRoyalty
          video {
            title
          }
        }
        previousNftOwner {
          __typename
        }
      }
      ... on BidMadeCompletingAuctionEventData {
        previousNftOwner {
          __typename
        }
        winningBid {
          id
          amount
          bidder {
            controllerAccount
          }
          nft {
            creatorRoyalty
            video {
              title
            }
          }
          bidder {
            controllerAccount
          }
          amount
        }
      }
      ... on EnglishAuctionSettledEventData {
        previousNftOwner {
          __typename
        }
        winningBid {
          nft {
            creatorRoyalty
            video {
              title
            }
          }
          bidder {
            controllerAccount
          }
          amount
        }
      }
      ... on OpenAuctionBidAcceptedEventData {
        winningBid {
          nft {
            creatorRoyalty
            video {
              title
            }
          }
          bidder {
            controllerAccount
          }
          amount
        }
      }
      ... on OpenAuctionBidAcceptedEventData {
        previousNftOwner {
          __typename
        }
        winningBid {
          amount
          bidder {
            controllerAccount
          }
          nft {
            creatorRoyalty
            video {
              title
            }
          }
        }
      }

      ... on ChannelRewardClaimedEventData {
        amount
      }
      ... on ChannelFundsWithdrawnEventData {
        amount
        actor {
          __typename
          ... on ContentActorCurator {
            __typename
          }
          ... on ContentActorLead {
            __typename
          }
          ... on ContentActorMember {
            member {
              controllerAccount
            }
          }
        }
      }
      ... on ChannelPaymentMadeEventData {
        amount
        rationale
        payer {
          controllerAccount
        }
      }
      ... on ChannelPaymentMadeEventData {
        amount
        rationale
        payer {
          controllerAccount
        }
      }
    }
  }
}

query GetTopSellingChannelsFromThreePeriods($limit: Int!, $where: ExtendedChannelWhereInput) {
  topAllTimeSellingChannels: topSellingChannels(where: $where, limit: $limit, periodDays: 0) {
    amount
    nftSold
    channel {
      ...BasicChannelFields
    }
  }
  # week
  topWeekSellingChannels: topSellingChannels(where: $where, limit: $limit, periodDays: 7) {
    amount
    nftSold
    channel {
      ...BasicChannelFields
    }
  }
  # month
  topMonthSellingChannels: topSellingChannels(where: $where, limit: $limit, periodDays: 30) {
    amount
    nftSold
    channel {
      ...BasicChannelFields
    }
  }
}
