// const { transport, emailTemplate } = require('./mail')

import { IResolvers } from 'apollo-server-express'
import { checkPermission } from '../user/resolvers'
//const LoginError = new Error('You must be logged in.')
//const PermissionError = new Error('Insufficient permissions.')

export const resolvers: IResolvers = {
  Query: {
    publishedTrainings: async (parent, args, context, info) => {
      checkPermission(context)
      return context.db.query.trainings(
        {
          where: { published: true },
          orderBy: 'trainingDate_DESC',
          first: 10,
          ...args,
        },
        info
      )
    },
    training: async (parent, args, context, info) => {
      checkPermission(context)
      if (args && args.id) {
        console.log('?', args)
        return context.db.query.training({ where: args }, info)
      } else {
        const trainingsArray = await context.db.query.trainings(
          {
            where: { published: true },
            orderBy: 'trainingDate_DESC',
            first: 1,
          },
          info
        )
        console.log(trainingsArray)
        if (trainingsArray && trainingsArray.length > 0) return trainingsArray[0]
      }
    },
    trainings: async (parent, args, context, info) => {
      checkPermission(context, ['INSTRUCTOR', 'ADMIN'])
      return context.db.query.trainings(args, info)
    },
    trainingsCount: async (parent, args, context, info) => {
      checkPermission(context)
      return await context.db.query.trainingsConnection(args, '{ aggregate { count } }')
    },
    trainingTypes: async (parent, args, context, info) => {
      checkPermission(context)
      return context.db.query.trainingTypes({}, info)
    },
    blocks: async (parent, args, context, info) => {
      checkPermission(context)
      return context.db.query.blocks({}, info)
    },
    formats: async (parent, args, context, info) => {
      checkPermission(context)
      return context.db.query.formats({}, info)
    },
    exercises: async (parent, args, context, info) => {
      checkPermission(context)
      return context.db.query.exercises({}, info)
    },
  },

  Mutation: {
    createTraining: async (parent, args, context, info) => {
      checkPermission(context, ['INSTRUCTOR', 'ADMIN'])
      const training = await context.db.mutation.createTraining({ data: args }, info)
      return training
    },
    updateTraining: async (parent, args, context, info) => {
      checkPermission(context, ['INSTRUCTOR', 'ADMIN'])
      const training = await context.db.mutation.updateTraining(args, info)
      return training
    },
    createTrainingType: async (parent, args, context, info) => {
      checkPermission(context, ['INSTRUCTOR', 'ADMIN'])
      const trainingType = await context.db.mutation.createTrainingType({ data: args }, info)
      return trainingType
    },
    // createBlock: async (parent, args, context, info) => {
    //   checkPermission(context, ['INSTRUCTOR', 'ADMIN'])
    //   const block = await context.db.mutation.createBlock({ data: args }, info)
    //   return block
    // },
    createFormat: (parent, args, context, info) => {
      checkPermission(context, ['INSTRUCTOR', 'ADMIN'])
      return context.db.mutation.createFormat({ data: args }, info)
    },
    register: (parent, args, context, info) => {
      checkPermission(context)
      return context.db.mutation.updateTraining({
        where: { id: args.training },
        data: { registrations: { connect: { id: context.req.userId } } },
      })
    },
    deregister: (parent, args, context, info) => {
      checkPermission(context)
      return context.db.mutation.updateTraining({
        where: { id: args.training },
        data: { registrations: { disconnect: { id: context.req.userId } } },
      })
    },
    publish: (parent, args, context, info) => {
      checkPermission(context, ['INSTRUCTOR'])
      return context.db.mutation.updateTraining({
        where: { id: args.training },
        data: { published: args.status },
      })
    },
  },
}
