// 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: {
    training: async (parent, args, context, info) => {
      checkPermission(context)
      return context.db.query.training({ where: args }, info)
    },
    trainings: async (parent, args, context, info) => {
      checkPermission(context)
      console.log(info)
      return context.db.query.trainings({}, info)
    },
    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)
    }
  },

  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: async (parent, args, context, info) => {
      checkPermission(context, ['INSTRUCTOR', 'ADMIN'])
      const block = await context.db.mutation.createFormat({ data: args }, info)
      return block
    },
    register: async (parent, args, context, info) => {
      checkPermission(context)
      const training = await context.db.query.training(
        { where: { id: args.training } },
        '{ id }'
      )
      if (!training) throw Error(`Training ${args.training} not found`)
      return context.db.mutation.updateTraining({
        where: { id: training.id },
        data: { registrations: { connect: { id: context.req.userId } } }
      })
    },
    deregister: async (parent, args, context, info) => {
      checkPermission(context)
      const training = await context.db.query.training(
        { where: { id: args.training } },
        '{ id }'
      )
      if (!training) throw Error(`Training ${args.training} not found`)
      return context.db.mutation.updateTraining({
        where: { id: training.id },
        data: { registrations: { disconnect: { id: context.req.userId } } }
      })
    }
  }
}
