// const { transport, emailTemplate } = require('./mail')
import { QueryResolvers, MutationResolvers, Resolvers } from '../gql/resolvers'
import { checkPermission } from '../user/resolvers'

const Query: QueryResolvers = {
  trainingArchive: async (parent, { first = 4, skip = 0 }, context, info) => {
    const filter = { published: true }
    const trainings = await context.db.query.trainings(
      { where: filter, orderBy: 'trainingDate_DESC', skip, first },
      '{ id }'
    )
    const trainingsConnection = await context.db.query.trainingsConnection(
      { where: filter },
      '{ aggregate { count } }'
    )
    console.log(first, skip, trainings, trainingsConnection)
    return {
      totalCount: trainingsConnection.aggregate.count,
      trainings: trainings.map((training: any) => training.id),
    }
  },
  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)
  },
}

const Mutation: MutationResolvers = {
  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
  },
  deleteTraining: (parent, args, context, info) => {
    checkPermission(context, ['INSTRUCTOR', 'ADMIN'])
    return context.db.mutation.deleteTraining({ where: { id: args.id } })
  },
  createTrainingType: (parent, args, context, info) => {
    checkPermission(context, ['INSTRUCTOR', 'ADMIN'])
    return context.db.mutation.createTrainingType({ data: args }, info)
  },
  // 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 },
    })
  },
}

export const resolvers: Resolvers = {
  Query,
  Mutation,
  TrainingArchive: {
    trainings: async ({ trainings }, args, context, info) => {
      const trainingDetails = await context.db.query.trainings(
        { where: { id_in: trainings } },
        info
      )
      return trainingDetails.map((trainingDetail: any, index: number) => {
        return { ...trainingDetail, __extras__: trainings[index] }
      })
    },
  },
  TrainingArchiveItem: {
    duration: (parent, args, context, info) => {
      return 2
    },
    blocks: async ({ __extras__ }: any, args, context, info) => {
      console.log(__extras__, args)
      const blockInstances = await context.db.query.blockInstances(
        { where: { parentTraining: { id: __extras__ } } },
        '{ block { title } }'
      )
      console.log(blockInstances)
      return blockInstances.map((blockInstance: any) => blockInstance.block.title)
    },
  },
}
