// import the required GraphQL Types
const {
    GraphQLObjectType,
    GraphQLInt,
    GraphQLString,
    GraphQLList,
    GraphQLID,
    GraphQLNonNull,
    GraphQLInputObjectType
  } = require('graphql');

  var GraphQLDate = require('graphql-date')    

const  db  = require('../../models');
const bcrypt = require('bcrypt')
const User = db.users;
const Ad = db.ads;
const UserType = require('./../types/userType')  

const  createJwtToken  = require("../../util/auth");
const AdType = require('../types/adType');

exports.createUserMutation = {
    type : UserType,
    description: 'Create a new user',
    args:{
        username: {
            type: GraphQLNonNull(GraphQLString)
        },
        email: {
          type: GraphQLNonNull(GraphQLString)
        },
        password: {
            type: GraphQLNonNull(GraphQLString)
        }
    },
    async resolve (parent, args ) {
    const hashedPassword = await bcrypt.hash(args.password, 10)
    args.password= hashedPassword    
    const user = new User(args)   

     await user.save()
    
        const token = createJwtToken(user)
        
        return token
    } 
}; 

exports.ask ={
  type : GraphQLString,
    description: 'ask a question',
    args:{
        ad_id: {
            type: GraphQLNonNull(GraphQLString)
        },
        username: {
            type: GraphQLNonNull(GraphQLString)
        },
        question: {
          type: GraphQLNonNull(GraphQLString)
      }
    },
    async resolve (parent, args ) {
    const ad = await Ad.findById(args.ad_id).exec(); 
    const question = {username:args.username, question:args.question}
    ad.questions.push(question)
    await Ad.findByIdAndUpdate(args.ad_id, ad, { runValidators: true })  

      return "Question asked successfully"
    } 
}


exports.answer ={
  type : GraphQLString,
    description: 'Create a new user',
    args:{
        user_id: {
            type: GraphQLNonNull(GraphQLString)
        },
        ad_id: {
            type: GraphQLNonNull(GraphQLString)
        },
        question_id: {
          type: GraphQLNonNull(GraphQLString)
      },
        answer: {
          type: GraphQLNonNull(GraphQLString)
      }
    },

    async resolve (parent, args, req ) {
    const user = req.verifiedUser    
    if(!user){
      return new Error("No one is connected")
    } 

    if(args.user_id != req.verifiedUser._id){
      return new Error("This Ad belongs to another user")
    }

    const ad = await Ad.findById(args.ad_id).exec(); 
    ad.questions.find(({id}) => id==args.question_id).reponse = args.answer
    
    await Ad.findByIdAndUpdate(args.ad_id, ad, { runValidators: true })  

      return "Question answered successfully"
    } 
}




exports.login = {
    type: GraphQLString,
    description: "Login user",
    args: {
      email: { type: GraphQLNonNull(GraphQLString) },
      password: { type: GraphQLNonNull(GraphQLString) },
    },
    async resolve(parent, args) {
      const user = await User.findOne({ email: args.email }).select("+password")
      console.log(user)
      const compare = await bcrypt.compare(args.password, user.password)
      if (!user || !compare) {
        throw new Error("Invalid credentials")
      }
  
      const token = createJwtToken(user)
      return token
    },
  }