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

const setter = ({
  activityID: {
    type: GraphQLString,
    description: 'id',
  },
  name: {
    type: GraphQLString,
    description: 'clientID',
  },
  skills: {
    type: new GraphQLList(GraphQLString),
    description: 'skills name',
  },
  description: {
    type: GraphQLString,
    description: 'activity description',
  },
  activity: {
    type: GraphQLString,
    description: 'kind fun or pro',
  },
  type: {
    type: GraphQLString,
    description: 'search or request',
  },
  userID: {
    type: GraphQLString,
    description: 'user id',
  },
  active: {
    type: GraphQLBoolean,
    description: 'is active',
  },
  matchedActivitiesID: {
    type: new GraphQLList(GraphQLString),
    description: 'matched activities',
  },
  score: {
    type: GraphQLString,
    description: 'score',
  },
});

const getter = ActivitiesType => ({
  ...setter,
  id: globalIdField('Activity'),
  matchedActivities: {
    type: new GraphQLList(ActivitiesType),
    resolve: async (obj, args, { loaders }) => {
      if (!obj.matchedActivitiesID) return null;
      const activities = await loaders.activity.loadManyByURL(obj.matchedActivitiesID);
      return activities;
    },
  },
  score: {
    ...setter.score,
    args: {
      id: { type: GraphQLString },
    },
    resolve: async (obj, args, { loaders }) => {
      if (!args.id) return null;
      const score = await loaders.score.load(args.id + obj.activityID);
      return score.score;
    },
  },
});

const getActivityType = (name) => {
  const ActivityType = new GraphQLObjectType({
    name,
    description: 'Activities',
    fields: () => getter(ActivityType),
  });
  return ActivityType;
};

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

export default {
  getActivityType,
  getActivityInputType,
};

