import { GraphQLObjectType, GraphQLString, GraphQLInputObjectType, GraphQLList } from 'graphql';
import { globalIdField } from 'graphql-relay';

import { getActivityType } from '../Activity';

const setter = ({
  isSocial: {
    type: GraphQLString,
    description: 'is social account',
  },
  clientID: {
    type: GraphQLString,
    description: 'clientID',
  },
  displayName: {
    type: GraphQLString,
    description: 'display name',
  },
  lastName: {
    type: GraphQLString,
    description: 'last name',
  },
  firstName: {
    type: GraphQLString,
    description: 'first name',
  },
  email: {
    type: GraphQLString,
    description: 'email',
  },
  nickname: {
    type: GraphQLString,
    description: 'user name',
  },
  token: {
    type: GraphQLString,
    description: 'token',
  },
  locale: {
    type: GraphQLString,
    description: 'locals ex: fr',
  },
  provider: {
    type: GraphQLString,
    description: 'auth provider',
  },
  picture: {
    type: GraphQLString,
    description: 'picture',
  },
  updatedAT: {
    type: GraphQLString,
    description: 'updated at',
  },
  userID: {
    type: GraphQLString,
    description: 'user id',
  },
  activitiesID: {
    type: new GraphQLList(GraphQLString),
    description: 'activities id',
  },
});

const getter = ({
  ...setter,
  id: globalIdField('User'),
  activities: {
    type: new GraphQLList(getActivityType('OwnActivities')),
    description: 'activities id',
    resolve: async (obj, args, { loaders }) => {
      if (!obj.activitiesID) return null;
      const activities = await loaders.activity.loadManyByURL(obj.activitiesID);
      return activities;
    },
  },
});

const getUserType = (nodeInterface) => {
  const UserType = new GraphQLObjectType({
    name: 'User',
    description: 'users',
    fields: getter,
    interfaces: [nodeInterface],
  });
  return UserType;
};

const getUserInputType = () => new GraphQLInputObjectType({
  name: 'UserInput',
  fields: setter,
});

export default { getUserType, getUserInputType };
