import { gql } from '@apollo/client/core';

// Profile fragments - exact from Proxyman
export const PROFILE_LOCATION_FRAGMENT = gql`
  fragment ProfileLocationFragment on ProfileLocation {
    ... on DeviceLocation {
      device {
        latitude
        longitude
        geocode {
          city
          country
          __typename
        }
        __typename
      }
      __typename
    }
    ... on VirtualLocation {
      core
      __typename
    }
    ... on TeleportLocation {
      current: device {
        city
        country
        __typename
      }
      teleport {
        latitude
        longitude
        geocode {
          city
          country
          __typename
        }
        __typename
      }
      __typename
    }
    __typename
  }
`;

export const PICTURE_FRAGMENT = gql`
  fragment GetPictureUrlFragment on Picture {
    id
    publicId
    pictureIsSafe
    pictureIsPrivate
    pictureUrl
    pictureUrls {
      small
      medium
      large
      __typename
    }
    __typename
  }
`;

export const PHOTO_CAROUSEL_FRAGMENT = gql`
  fragment PhotoCarouselPictureFragment on Picture {
    id
    pictureIsPrivate
    pictureIsSafe
    pictureStatus
    pictureType
    pictureUrl
    pictureUrls {
      small
      medium
      large
      __typename
    }
    publicId
    verification {
      status
      __typename
    }
    __typename
  }
`;

export const PROFILE_INTERACTION_STATUS_FRAGMENT = gql`
  fragment ProfileInteractionStatusFragment on Profile {
    interactionStatus {
      message
      mine
      theirs
      __typename
    }
    __typename
  }
`;

export const ANALYTICS_PROFILE_FRAGMENT = gql`
  fragment AnalyticsProfileFragment on Profile {
    id
    isUplift
    lastSeen
    age
    gender
    sexuality
    verificationStatus
    connectionGoals
    hasHiddenBio
    hiddenBio
    imaginaryName
    interactionStatus {
      mine
      theirs
      message
      __typename
    }
    distance {
      km
      mi
      __typename
    }
    profilePairs {
      identityId
      __typename
    }
    metadata {
      source
      __typename
    }
    __typename
  }
`;

export const PARTNER_FRAGMENT = gql`
  ${PICTURE_FRAGMENT}
  ${PROFILE_INTERACTION_STATUS_FRAGMENT}
  fragment Partner on Partner {
    partnerId
    partnerLabel
    partnerProfile {
      id
      age
      imaginaryName
      dateOfBirth
      gender
      sexuality
      isIncognito
      photos {
        ...GetPictureUrlFragment
        __typename
      }
      ...ProfileInteractionStatusFragment
      status
      verificationStatus
      isMajestic
      __typename
    }
    partnerStatus
    __typename
  }
`;

export const CONSTELLATION_FRAGMENT = gql`
  ${PARTNER_FRAGMENT}
  fragment Constellation on Profile {
    constellation {
      ...Partner
      __typename
    }
    __typename
  }
`;

// Main queries - exact from Proxyman
export const PROFILE_QUERY = gql`
  ${PROFILE_LOCATION_FRAGMENT}
  ${PHOTO_CAROUSEL_FRAGMENT}
  ${CONSTELLATION_FRAGMENT}
  ${ANALYTICS_PROFILE_FRAGMENT}
  query ProfileQuery($profileId: String!) {
    profile(id: $profileId) {
      bio
      hiddenBio
      hasHiddenBio
      age
      streamUserId
      dateOfBirth
      distance {
        km
        mi
        __typename
      }
      connectionGoals
      desires
      lookingFor
      ageRange
      distanceMax
      recentlyOnline
      gender
      id
      status
      imaginaryName
      interactionStatus {
        message
        mine
        theirs
        __typename
      }
      interests
      isMajestic
      isIncognito
      lastSeen
      location {
        ...ProfileLocationFragment
        __typename
      }
      sexuality
      photos {
        ...PhotoCarouselPictureFragment
        __typename
      }
      ...Constellation
      allowPWM
      verificationStatus
      enableChatContentModeration
      ...AnalyticsProfileFragment
      isParticipantToEventChat
      __typename
    }
  }
`;

export const DISCOVER_PROFILES_QUERY = gql`
  ${PICTURE_FRAGMENT}
  ${PROFILE_INTERACTION_STATUS_FRAGMENT}
  ${ANALYTICS_PROFILE_FRAGMENT}
  query DiscoverProfiles($input: ProfileDiscoveryInput!) {
    discovery(input: $input) {
      nodes {
        id
        age
        imaginaryName
        gender
        sexuality
        isIncognito
        isMajestic
        verificationStatus
        connectionGoals
        desires
        bio
        interests
        distance {
          km
          mi
          __typename
        }
        photos {
          ...GetPictureUrlFragment
          pictureType
          __typename
        }
        ...ProfileInteractionStatusFragment
        ...AnalyticsProfileFragment
        __typename
      }
      hasNextBatch
      __typename
    }
  }
`;

export const WHO_LIKES_ME_QUERY = gql`
  ${PICTURE_FRAGMENT}
  ${PROFILE_INTERACTION_STATUS_FRAGMENT}
  ${ANALYTICS_PROFILE_FRAGMENT}
  query WhoLikesMe($sortBy: SortBy!, $limit: Int, $cursor: String) {
    interactions: whoLikesMe(input: { sortBy: $sortBy }, limit: $limit, cursor: $cursor) {
      nodes {
        id
        age
        imaginaryName
        gender
        sexuality
        isIncognito
        isMajestic
        verificationStatus
        desires
        connectionGoals
        distance {
          km
          mi
          __typename
        }
        photos {
          ...GetPictureUrlFragment
          pictureType
          __typename
        }
        ...ProfileInteractionStatusFragment
        ...AnalyticsProfileFragment
        __typename
      }
      pageInfo {
        total
        hasNextPage
        nextPageCursor
        __typename
      }
      __typename
    }
  }
`;

export const WHO_PINGS_ME_QUERY = gql`
  ${PROFILE_LOCATION_FRAGMENT}
  ${PHOTO_CAROUSEL_FRAGMENT}
  query WhoPingsMe($sortBy: SortBy!, $limit: Int, $cursor: String) {
    interactions: whoPingsMe(
      input: { sortBy: $sortBy }
      limit: $limit
      cursor: $cursor
    ) {
      nodes {
        id
        age
        gender
        status
        lastSeen
        desires
        connectionGoals
        isUplift
        sexuality
        isMajestic
        dateOfBirth
        streamUserId
        imaginaryName
        bio
        hiddenBio
        hasHiddenBio
        allowPWM
        interests
        verificationStatus
        interactionStatus {
          message
          mine
          theirs
          __typename
        }
        profilePairs {
          identityId
          __typename
        }
        distance {
          km
          mi
          __typename
        }
        location {
          ...ProfileLocationFragment
          __typename
        }
        photos {
          ...PhotoCarouselPictureFragment
          __typename
        }
        __typename
      }
      pageInfo {
        total
        hasNextPage
        nextPageCursor
        __typename
      }
      __typename
    }
  }
`;

export const HEADER_SUMMARIES_QUERY = gql`
  query HeaderSummaries($limit: Int = 10, $cursor: String) {
    summaries: getChatSummariesForChatHeader(limit: $limit, cursor: $cursor) {
      nodes {
        id
        name
        type
        status
        avatarSet
        chatAvatarUrl
        chatDescription
        openedAt
        memberCount
        latestMessage
        streamChannelId
        targetProfileId
        targetProfile {
          isVerified
          isMajestic
          gender
          age
          sexuality
          __typename
        }
        enableChatContentModeration
        participationStatus
        participationType
        __typename
      }
      pageInfo {
        hasNextPage
        nextPageCursor
        __typename
      }
      __typename
    }
  }
`;

export const LIST_SUMMARIES_QUERY = gql`
  query ListSummaries($limit: Int = 30, $cursor: String) {
    summaries: getChatSummariesForChatList(limit: $limit, cursor: $cursor) {
      nodes {
        id
        name
        type
        status
        avatarSet
        chatAvatarUrl
        chatDescription
        openedAt
        memberCount
        latestMessage
        streamChannelId
        targetProfileId
        targetProfile {
          isVerified
          isMajestic
          gender
          age
          sexuality
          __typename
        }
        enableChatContentModeration
        participationStatus
        participationType
        __typename
      }
      pageInfo {
        hasNextPage
        nextPageCursor
        __typename
      }
      __typename
    }
  }
`;

export const GET_CHAT_SUMMARY_QUERY = gql`
  query GetChatSummary($streamChatId: String!) {
    summary: getChatSummary(streamChatId: $streamChatId) {
      id
      name
      type
      status
      avatarSet
      chatAvatarUrl
      chatDescription
      openedAt
      memberCount
      latestMessage
      streamChannelId
      targetProfileId
      targetProfile {
        isVerified
        isMajestic
        gender
        age
        sexuality
        __typename
      }
      enableChatContentModeration
      participationStatus
      participationType
      __typename
    }
  }
`;

export const DISCOVER_SEARCH_SETTINGS_QUERY = gql`
  query DiscoverSearchSettingsQuery($profileId: String!) {
    profile(id: $profileId) {
      id
      ageRange
      distanceMax
      lookingFor
      desiringFor
      recentlyOnline
      __typename
    }
  }
`;

export const AVAILABLE_DESIRES_QUERY = gql`
  query AvailableDesires($locale: String!, $version: Int!) {
    availableDesires(locale: $locale, version: $version) {
      id
      name
      description
      category
      __typename
    }
  }
`;

export const IS_INCOGNITO_QUERY = gql`
  query IsIncognitoQuery($profileId: String!) {
    profile(id: $profileId) {
      id
      isIncognito
      __typename
    }
  }
`;

// Query for Stream Chat credentials
export const STREAM_CREDENTIALS_QUERY = gql`
  query StreamCredentialsQuery($profileId: String!) {
    profile(id: $profileId) {
      id
      streamToken
      streamUserId
      imaginaryName
      __typename
    }
  }
`;

export const AUTH_PROVIDER_QUERY = gql`
  query AuthProviderQuery {
    account {
      id
      status
      profiles {
        id
        status
        imaginaryName
        __typename
      }
      __typename
    }
  }
`;

export const ACCOUNT_STATUS_QUERY = gql`
  ${PICTURE_FRAGMENT}
  ${PROFILE_INTERACTION_STATUS_FRAGMENT}
  ${CONSTELLATION_FRAGMENT}
  query AuthProviderStatusSyncQuery {
    account {
      id
      status
      isFinishedOnboarding
      isMajestic
      availablePings
      upliftExpirationTimestamp
      isUplift
      profiles {
        id
        profileLinks(linkType: PAIR) {
          linkId
          linkType
          profileId
          __typename
        }
        ...Constellation
        __typename
      }
      __typename
    }
  }
`;
