import { makeExecutableSchema } from '@graphql-tools/schema'
import type { ExecutionResult } from '@graphql-tools/utils'
import actionLogTodoQL from '../../schemas/actionLogTodo.graphql'
import { executeRead, executeWrite } from '../utils'
import type { Resolvers } from './actionLogTodoType'
import type { Context, SubschemaConfig, SubschemaResolverType } from './typing'

const actionLogTodoSchema: SubschemaConfig = {
  merge: {
    EmailThreadType: {
      fieldName: 'archiveEmail',
      selectionSet: '{ id }',
      args: ({ id }) => ({ id }),
    },
    EmailType: {
      fieldName: 'emailTodo',
      selectionSet: '{ id }',
      args: ({ id }) => ({ id }),
    },
    ActionLogType: {
      fieldName: 'todoActionLog',
      selectionSet: '{ id }',
      args: ({ id }) => ({ id }),
    },
    TeamQuestionnaireStateType: {
      fieldName: 'teamQuestionnaireTodo',
      selectionSet: '{ id }',
      args: ({ id }) => ({ id }),
    },
  },
  schema: makeExecutableSchema<Context>({
    typeDefs: [actionLogTodoQL],
    resolvers: {
      Query: {
        archiveEmail: async (_, { id }, { exerciseId }) => {
          const { data } = (await executeRead({
            exerciseId,
            key: 'emailThreadArchive',
            subkey: [id],
          })) as ExecutionResult<{ readValue: string | null }>
          return {
            archived: JSON.parse(data?.readValue || 'false'),
            id: id,
          }
        },
        emailTodo: async (_, { id }, { exerciseId }) => {
          const { data } = (await executeRead({
            exerciseId,
            key: 'emailTodo',
            subkey: [id],
          })) as ExecutionResult<{ readValue: string | null }>
          return {
            todo: JSON.parse(data?.readValue || 'false'),
            id: id,
          }
        },
        todoActionLog: async (_, { id }, { exerciseId }) => {
          const { data } = (await executeRead({
            exerciseId,
            key: 'actionLogTodo',
            subkey: [id],
          })) as ExecutionResult<{ readValue: string | null }>
          return {
            todo: JSON.parse(data?.readValue || 'false'),
            id: id,
          }
        },
        teamQuestionnaireTodo: async (_, { id }, { exerciseId }) => {
          const { data } = (await executeRead({
            exerciseId,
            key: 'questionnaireTodo',
            subkey: [id],
          })) as ExecutionResult<{ readValue: string | null }>
          return {
            todo: JSON.parse(data?.readValue || 'false'),
            id,
          }
        },
      },
      Mutation: {
        setTodoActionLog: (_, { actionLogId, state }, { exerciseId }) => {
          executeWrite({
            exerciseId,
            key: 'actionLogTodo',
            value: JSON.stringify(state),
            subkey: [actionLogId],
          })
          return {
            id: actionLogId,
            todo: state,
          }
        },
        setEmailTodo: (_, { emailId, state }, { exerciseId }) => {
          executeWrite({
            exerciseId,
            key: 'emailTodo',
            value: JSON.stringify(state),
            subkey: [emailId],
          })
          return {
            id: emailId,
            todo: state,
          }
        },
        setTeamQuestionnaireTodo: (
          _,
          { teamQuestionnaireId, state },
          { exerciseId }
        ) => {
          executeWrite({
            exerciseId,
            key: 'questionnaireTodo',
            value: JSON.stringify(state),
            subkey: [teamQuestionnaireId],
          })
          return {
            id: teamQuestionnaireId,
            todo: state,
          }
        },
        setArchiveEmail: (_, { emailThreadId, state }, { exerciseId }) => {
          executeWrite({
            exerciseId,
            key: 'emailThreadArchive',
            value: JSON.stringify(state),
            subkey: [emailThreadId],
          })
          return {
            id: emailThreadId,
            archived: state,
          }
        },
      },
    } as SubschemaResolverType<Resolvers>,
  }),
}

export default actionLogTodoSchema
