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

  var GraphQLDate = require('graphql-date')    

const  db  = require('../../models');

const Ad = db.ads;
const AdType = require('./../types/adType')  


//delete one Ad
exports.deleteAdMutation = {
    type : AdType,
    description: 'Delete an Ad',
    args:{
        id : {
            type:GraphQLID
        }
    },
    resolve: async (parent, args ) => await Ad.findByIdAndRemove(args.id).exec()
}; 




//use it multiple times
const ad = {
    titre: {
        type: GraphQLString
    },
    slug: {
      type: GraphQLString
  },
  type: {
      type: GraphQLString
  },
  statusPublication: {
      type: GraphQLString
  },
  statusBien: {
      type: GraphQLString
  },
  description: {
      type: GraphQLString
  },
  prix: {
      type: GraphQLInt
  },
  //error
  date: {
      type: GraphQLDate
  },
  images:{
      type: GraphQLList(GraphQLString)
  },
}

exports.createAdMutation = {
    type : AdType,
    description: 'Create an Ad',
    args:ad,
    resolve: async (parent, args ) =>{
    const ad = new Ad(args)    
    const newAd = await ad.save()
    if(!newAd){
        throw new Error('Ad is empty');
    }else{
        return newAd
    }

    } 
}; 

  


exports.updateAdMutation = {
    type : AdType,
    description: 'Create an Ad',
    args:{
        id: {
            type: GraphQLID
        },
        titre: {
            type: GraphQLString
        },
        slug: {
          type: GraphQLString
      },
      type: {
          type: GraphQLString
      },
      statusPublication: {
          type: GraphQLString
      },
      statusBien: {
          type: GraphQLString
      },
      description: {
          type: GraphQLString
      },
      prix: {
          type: GraphQLInt
      },
      //error
      date: {
          type: GraphQLDate
      },
      images:{
          type: GraphQLList(GraphQLString)
      },
    },
    resolve: async (parent, args ) =>{
       
    const updateAd = await Ad.findByIdAndUpdate(args.id,args)
    if(!updateAd){
        throw new Error('Ad is empty');
    }else{
        return args
    }
    } 
};
