import { gql, actionPromise } from "./index";

export const gqlAboutUser = (_id) =>
  gql(
    `query AboutMe($userId:String){
            UserFindOne(query:$userId)
            {
              _id createdAt login nick avatar{_id url} 
              likes {_id}
              followers{_id login nick avatar{_id url}} 
              following{_id login nick avatar{_id url}}
            }
          }`,
    {
      userId: JSON.stringify([{ _id }]),
    }
  );

  export const gqlFollowUser = (userId, followerId) =>
  actionPromise(
    "followUser",
    gql(
      `
        mutation FollowUser($user: UserInput) {
          UserUpsert(user: $user) {
            _id
          }
        }
      `,
      {
        user: {
          _id: userId,
          following: [{ _id: followerId }],
        },
      }
    )
  );

  export const actionSubscribe = (token,followingIds) => {
    console.log("subscribe");
    return actionPromise(
      "Subscribe",
      gql(
        `mutation following($user:UserInput){
          UserUpsert( user:$user){
              following {
                _id
                nick
              }
          }
        }`,
        {
          user: {
            _id: token,
            following: followingIds
          },
        }
      )
    );
  };

  export const actionUnfollow = (token,id) => {
    return actionPromise(
      "unfollow",
      gql(
        `mutation unfollow($user:UserInput){
          UserDelete( user:$user){
              following {
                _id
              }
          }
        }`,
        {
          user: {
            _id: token,
            following: {_id:id}
          },
        }
      )
    );
  };
  

  export const getUserFollowing = (token) => {
    return (
      gql(
        `query getFollowing($_id: String) {
           UserFindOne(query: $_id) {
             following{
              _id
             }
           }
         }`,
        {
          _id: JSON.stringify([{ _id: token }])
        }
      )
    )
  }
  







export const gqlUnfollowUser = (userId) =>
  actionPromise(
    "unfollowUser",
    gql(
      `mutation UnfollowUser($_id: String!) {
          UserUpsert(user: { following: [], _id: $_id }) {
            _id
          }
        }`,
      { _id: userId }
    )
  );
