import { gql } from '@apollo/client/core';
import {
  LIKES_PROFILE_FRAGMENT,
  PROFILE_LOCATION_FRAGMENT,
  SEARCH_SETTINGS_PROFILE_FRAGMENT,
} from './queries';

// Mutations - shapes verified against live app v8.11.0 capture

export const PROFILE_LIKE_MUTATION = gql`
  mutation ProfileLike($targetProfileId: String!) {
    profileLike(input: { targetProfileId: $targetProfileId }) {
      status
      chat {
        id
        name
        type
        streamChatId
        status
        members {
          id
          status
          analyticsId
          imaginaryName
          streamUserId
          age
          dateOfBirth
          sexuality
          isIncognito
          gender
          photos {
            id
            publicId
            pictureIsSafe
            pictureIsPrivate
            pictureUrl
            pictureUrls {
              small
              medium
              large
              __typename
            }
            pictureType
            __typename
          }
          __typename
        }
        __typename
      }
      __typename
    }
  }
`;

export const DEVICE_LOCATION_UPDATE_MUTATION = gql`
  ${PROFILE_LOCATION_FRAGMENT}
  mutation DeviceLocationUpdate($input: DeviceLocationInput!) {
    deviceLocationUpdate(input: $input) {
      id
      location {
        device {
          latitude
          longitude
          geocode {
            city
            country
            __typename
          }
          __typename
        }
        __typename
      }
      profiles {
        id
        location {
          ...ProfileLocationFragment
          __typename
        }
        __typename
      }
      __typename
    }
  }
`;

export const SEARCH_SETTINGS_UPDATE_MUTATION = gql`
  ${SEARCH_SETTINGS_PROFILE_FRAGMENT}
  mutation SearchSettingsUpdate(
    $ageRange: [Int]
    $distanceMax: Float
    $desiringFor: [Desire!]
    $lookingFor: [LookingFor!]
    $recentlyOnline: Boolean
    $lgbtqiaMode: LgbtqiaMode
  ) {
    profileUpdate(
      input: {
        ageRange: $ageRange
        distanceMax: $distanceMax
        desiringFor: $desiringFor
        lookingFor: $lookingFor
        recentlyOnline: $recentlyOnline
        lgbtqiaMode: $lgbtqiaMode
      }
    ) {
      ...SearchSettingsProfileFragment
      __typename
    }
  }
`;

export const LAST_SEEN_UPDATE_MUTATION = gql`
  mutation LastSeenProviderUpdateProfile($profileId: String!) {
    updatedProfileLastSeen: profileUpdateLastSeen(profileId: $profileId)
  }
`;

export const ACCOUNT_ADD_NOTIFICATION_TOKEN_MUTATION = gql`
  mutation AccountAddNotificationToken($token: String!) {
    accountAddNotificationToken(input: { token: $token })
  }
`;

export const PROFILE_PING_MUTATION = gql`
  mutation ProfilePing($targetProfileId: String!, $message: String, $overrideInappropriate: Boolean) {
    profilePing(
      input: {targetProfileId: $targetProfileId, message: $message, overrideInappropriate: $overrideInappropriate}
    ) {
      status
      chat {
        id
        name
        type
        streamChatId
        status
        members {
          id
          status
          analyticsId
          imaginaryName
          streamUserId
          age
          dateOfBirth
          sexuality
          isIncognito
          gender
          photos {
            id
            publicId
            pictureIsSafe
            pictureIsPrivate
            pictureUrl
            pictureUrls {
              small
              medium
              large
              __typename
            }
            pictureType
            __typename
          }
          __typename
        }
        disconnectedMembers {
          id
          __typename
        }
        __typename
      }
      account {
        id
        availablePings
        __typename
      }
      __typename
    }
  }
`;

export const PROFILE_UPDATE_MUTATION = gql`
  mutation ProfileUpdate($input: ProfileUpdateInput!) {
    profileUpdate(input: $input) {
      id
      age
      ageRange
      allowPWM
      bio
      hiddenBio
      hasHiddenBio
      completionStatus
      connectionGoals
      dateOfBirth
      desires
      distanceMax
      gender
      imaginaryName
      interests
      isIncognito
      lookingFor
      recentlyOnline
      sexuality
      status
      streamToken
      isParticipantToEventChat
      photos {
        id
        publicId
        pictureIsSafe
        pictureIsPrivate
        pictureUrl
        pictureUrls {
          small
          medium
          large
          __typename
        }
        __typename
      }
      __typename
    }
  }
`;

export const PROFILE_DISLIKE_MUTATION = gql`
  mutation ProfileDislike($targetProfileId: String!) {
    profileDislike(input: { targetProfileId: $targetProfileId })
  }
`;

// === New mutations discovered via API probing + APK analysis (v8.11.0) ===

// Location mutations
export const PROFILE_LOCATION_UPDATE_MUTATION = gql`
  mutation ProfileLocationUpdate($input: ProfileLocationInput!) {
    profileLocationUpdate(input: $input) {
      location {
        ... on DeviceLocation {
          device {
            latitude
            longitude
            geocode {
              city
              country
              __typename
            }
            __typename
          }
          __typename
        }
        ... on TeleportLocation {
          teleport {
            latitude
            longitude
            geocode {
              city
              country
              __typename
            }
            __typename
          }
          __typename
        }
        ... on VirtualLocation {
          core
          __typename
        }
        __typename
      }
      __typename
    }
  }
`;

// Interaction mutations
export const PROFILE_BLOCK_MUTATION = gql`
  mutation ProfileBlock($input: ProfileBlockInteractionInput!) {
    profileBlock(input: $input)
  }
`;

export const PROFILE_REPORT_MUTATION = gql`
  mutation ProfileReport($input: ProfileReportInteractionInput!) {
    profileReport(input: $input)
  }
`;

export const PROFILE_ACCEPT_PING_MUTATION = gql`
  mutation ProfileAcceptPing($targetProfileId: String!) {
    profileAcceptPing(input: { targetProfileId: $targetProfileId }) {
      status
      chat {
        id
        name
        type
        streamChatId
        status
        members {
          id
          status
          imaginaryName
          streamUserId
          age
          gender
          sexuality
          photos {
            id
            pictureUrl
            pictureUrls {
              small
              medium
              large
              __typename
            }
            __typename
          }
          __typename
        }
        __typename
      }
      __typename
    }
  }
`;

export const PROFILE_REJECT_PING_MUTATION = gql`
  mutation ProfileRejectPing($targetProfileId: String!) {
    profileRejectPing(input: { targetProfileId: $targetProfileId })
  }
`;

export const UNDO_PROFILE_DISLIKE_MUTATION = gql`
  mutation UndoProfileDislike {
    undoProfileDislike
  }
`;

// Account mutations
export const SYNC_ACCOUNT_MUTATION = gql`
  mutation SyncAccount {
    syncAccount {
      id
      status
      isMajestic
      isUplift
      availablePings
      __typename
    }
  }
`;

export const ACCOUNT_REDEEM_OFFER_MUTATION = gql`
  mutation RedeemAccountOffer($input: RedeemAccountOfferInput!) {
    accountRedeemOffer(input: $input) {
      redeemedOffers {
        offerName
        redeemedAt
        __typename
      }
      __typename
    }
  }
`;

export const ACCOUNT_REDEEM_OFFER_WITH_REFLECTION_MUTATION = gql`
  mutation AccountRedeemOfferWithReflection($input: RedeemAccountOfferInput!) {
    accountRedeemOfferWithReflection(input: $input) {
      availablePings
      __typename
    }
  }
`;

// Chat mutations
export const DISCONNECT_FROM_CHAT_MUTATION = gql`
  mutation DisconnectFromChat($input: ChatDisconnectInput!) {
    disconnectFromChat(input: $input) {
      chatId
      __typename
    }
  }
`;

export const CHAT_ACTIVATE_MUTATION = gql`
  mutation ChatActivate($input: ChatActivateInput!) {
    chatActivate(input: $input) {
      streamChatId
      __typename
    }
  }
`;

export const CHAT_CREATE_MUTATION = gql`
  mutation ChatCreate($input: ChatCreateInput!) {
    chatCreate(input: $input) {
      id
      name
      type
      status
      streamChannelId
      __typename
    }
  }
`;

export const CHATS_MARK_READ_MUTATION = gql`
  mutation ChatsMarkRead($input: ChatsMarkReadInput!) {
    chatsMarkRead(input: $input)
  }
`;

export const GROUP_CHAT_ADD_MEMBERS_MUTATION = gql`
  mutation GroupChatAddMembers($sourceProfileId: String!, $chatId: String!, $targetProfileIds: [String!]!) {
    groupChatAddMembers(
      input: { chatId: $chatId, targetProfileIds: $targetProfileIds }
    ) {
      id
      streamChatId
      name
      type
      status
      members {
        id
        imaginaryName
        __typename
      }
      __typename
    }
  }
`;

// Profile link mutations
export const PROFILE_LINK_CREATE_MUTATION = gql`
  mutation ProfileLinkCreate($input: ProfileLinkCreateInput!) {
    profileLinkCreate(input: $input) {
      linkId
      url
      linkType
      __typename
    }
  }
`;

export const PROFILE_LINK_DELETE_MUTATION = gql`
  mutation ProfileLinkDelete($linkId: String!) {
    profileLinkDelete(linkId: $linkId) {
      linkId
      __typename
    }
  }
`;

// Picture mutations
export const PICTURE_CREATE_MUTATION = gql`
  mutation PictureCreate($input: PictureCreateInput!) {
    pictureCreate(input: $input) {
      id
      publicId
      pictureUrl
      pictureUrls {
        small
        medium
        large
        __typename
      }
      pictureOrder
      pictureType
      __typename
    }
  }
`;

export const PICTURE_UPDATE_MUTATION = gql`
  mutation PictureUpdate($input: PictureUpdateInput!) {
    pictureUpdate(input: $input) {
      id
      publicId
      pictureType
      __typename
    }
  }
`;

export const PICTURE_DELETE_MUTATION = gql`
  mutation PictureDelete($input: PictureDeleteInput!) {
    pictureDelete(input: $input) {
      id
      __typename
    }
  }
`;

// Upload mutations
export const CLOUDINARY_GENERATE_UPLOAD_CREDENTIALS_MUTATION = gql`
  mutation CloudinaryGenerateUploadCredentials($resourceType: ResourceType) {
    cloudinaryGenerateUploadCredentials(resourceType: $resourceType) {
      publicId
      signature
      timestamp
      __typename
    }
  }
`;

export const CREATE_UPLOAD_URL_MUTATION = gql`
  mutation CreateUploadUrl($input: CreateUploadUrlInput!) {
    createUploadUrl(input: $input) {
      mediaUploadUrl
      __typename
    }
  }
`;

// Settings mutation
export const APP_SETTINGS_UPDATE_MUTATION = gql`
  mutation AppSettingsUpdate(
    $isDistanceInMiles: Boolean
    $receiveMarketingNotifications: Boolean
    $language: String
    $receiveNewsEmailNotifications: Boolean
    $receivePromotionsEmailNotifications: Boolean
    $receiveNewsPushNotifications: Boolean
    $receivePromotionsPushNotifications: Boolean
    $receiveNewConnectionPushNotifications: Boolean
    $receiveNewPingPushNotifications: Boolean
    $receiveNewMessagePushNotifications: Boolean
    $receiveNewLikePushNotifications: Boolean
  ) {
    accountUpdate(
      input: {
        isDistanceInMiles: $isDistanceInMiles
        receiveMarketingNotifications: $receiveMarketingNotifications
        language: $language
        receiveNewsEmailNotifications: $receiveNewsEmailNotifications
        receivePromotionsEmailNotifications: $receivePromotionsEmailNotifications
        receiveNewsPushNotifications: $receiveNewsPushNotifications
        receivePromotionsPushNotifications: $receivePromotionsPushNotifications
        receiveNewConnectionPushNotifications: $receiveNewConnectionPushNotifications
        receiveNewPingPushNotifications: $receiveNewPingPushNotifications
        receiveNewMessagePushNotifications: $receiveNewMessagePushNotifications
        receiveNewLikePushNotifications: $receiveNewLikePushNotifications
      }
    ) {
      id
      appSettings {
        receiveMarketingNotifications
        receiveNewsEmailNotifications
        receivePromotionsEmailNotifications
        receiveNewsPushNotifications
        receivePromotionsPushNotifications
        receiveNewConnectionPushNotifications
        receiveNewPingPushNotifications
        receiveNewMessagePushNotifications
        receiveNewLikePushNotifications
        __typename
      }
      __typename
    }
  }
`;

// Filtered interactions — both Likes and Pings tabs use these (mutations, not queries).
export const FILTERED_WHO_LIKES_ME_MUTATION = gql`
  ${LIKES_PROFILE_FRAGMENT}
  mutation FilteredWhoLikesMe($input: FilteredInteractionInput!, $cursor: String) {
    filteredWhoLikesMe(input: $input, cursor: $cursor) {
      filters {
        ageRange
        desires
        lookingFor
        sexualities
        __typename
      }
      profiles {
        nodes {
          ...LikesProfileFragment
          __typename
        }
        pageInfo {
          total
          unfilteredTotal
          hasNextPage
          nextPageCursor
          __typename
        }
        __typename
      }
      __typename
    }
  }
`;

export const FILTERED_WHO_PINGS_ME_MUTATION = gql`
  ${LIKES_PROFILE_FRAGMENT}
  mutation FilteredWhoPingsMe($input: FilteredPingInteractionInput!, $cursor: String) {
    filteredWhoPingsMe(input: $input, cursor: $cursor) {
      filters {
        ageRange
        __typename
      }
      profiles {
        nodes {
          ...LikesProfileFragment
          __typename
        }
        pageInfo {
          total
          unfilteredTotal
          hasNextPage
          nextPageCursor
          __typename
        }
        __typename
      }
      __typename
    }
  }
`;

// Challenge mutation
export const START_CHALLENGE_MUTATION = gql`
  mutation StartChallenge($input: StartChallengeInput!) {
    startChallenge(input: $input)
  }
`;

// Account management
export const ACCOUNT_DEACTIVATE_MUTATION = gql`
  mutation AccountDeactivate {
    accountDeactivate {
      id
      status
      __typename
    }
  }
`;

export const ACCOUNT_TERMINATE_MUTATION = gql`
  mutation AccountTerminate {
    accountTerminate {
      id
      status
      __typename
    }
  }
`;
