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

  var GraphQLDate = require('graphql-date')

const jwt = require("jsonwebtoken")
const { createJwtToken } = require("../../util/auth")

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

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


//delete one Ad
exports.deleteAdMutation = {
    type : GraphQLString,
    description: 'Delete an Ad',
    args:{
        id : {
            type: GraphQLNonNull(GraphQLID)
        }
    },
    async resolve (parent, args, { verifiedUser } ) {
        console.log(verifiedUser)
        if (!verifiedUser) {
        throw new Error("Unauthenticated")
        }
        const adDeleted = await Ad.findOneAndDelete({
            _id: args.id,
            id_user: verifiedUser._id,
          })
          if (!adDeleted) {
            throw new Error("No ad with the given ID found for the user")
          }
      
          return "ad Deleted"
    }
}; 


exports.createAdMutation = {
    type : AdType,
    description: 'Create an Ad',
    args : {
        titre: {
            type: GraphQLNonNull(GraphQLString)
        },
        slug: {
          type: GraphQLNonNull(GraphQLString)
        },
        type: {
            //type: GraphQLNonNull(GraphQLString)
            type: GraphQLNonNull(new GraphQLEnumType({
                name: 'type',
                values: {
                  Vente: { value: 'Vente' },
                  Location: { value: 'Location' }
                }
              }))
        },
        statusPublication: {
            //type: GraphQLNonNull(GraphQLString)
            type: GraphQLNonNull(new GraphQLEnumType({
                name: 'statusPublication',
                values: {
                  Publiee: { value: 'publiée' },
                  NonPubliee: { value: 'non publiée' }
                }
              }))
        },
        statusBien: {
            //type: GraphQLNonNull(GraphQLString)
            type: GraphQLNonNull(new GraphQLEnumType({
                name: 'statusBien',
                values: {
                  Disponible: { value: 'Disponible' },
                  Loue: { value: 'Loué' },
                  Vendu: { value: 'Vendu' },
                }
              }))
        },
        description: {
            type: GraphQLNonNull(GraphQLString)
        },
        prix: {
            type: GraphQLNonNull(GraphQLInt)
        },
        date: {
            type: GraphQLNonNull(GraphQLString)
        }, 
        images:{
            type: GraphQLNonNull(GraphQLList(GraphQLString))
        },
    },
    resolve(parent, args, req ){
        console.log("Verified User: ", req.verifiedUser)
        if (!req.verifiedUser) {
        throw new Error("Unauthorized")
        }
        args.id_user = req.verifiedUser._id
        const ad = new Ad(args)
          return ad.save()
    } 
}; 

  

exports.updateAdMutation = {
    type : AdType,
    description: 'Update an Ad',
    args:{
        id:{
            type: GraphQLNonNull(GraphQLString)
        },
        titre: {
            type: GraphQLString
        },
        slug: {
          type: GraphQLString
      },
      type: {
          //type: GraphQLString
          type: new GraphQLEnumType({
            name: 'typeUpdate',
                values: {
                  Vente: { value: 'Vente' },
                  Location: { value: 'Location' }
                }
          })
      },
      statusPublication: {
          //type: GraphQLString
          type: new GraphQLEnumType({
            name: 'statusPublicationUpdate',
            values: {
              Publiee: { value: 'publiée' },
              NonPubliee: { value: 'non publiée' }
            }
          })
      },
      statusBien: {
          //type: GraphQLString
          type: new GraphQLEnumType({
            name: 'statusBienUpdate',
            values: {
              Disponible: { value: 'Disponible' },
              Loue: { value: 'Loué' },
              Vendu: { value: 'Vendu' },
            }
          })
      },
      description: {
          type: GraphQLString
      },
      prix: {
          type: GraphQLInt
      },
      date: {
          type: GraphQLDate
      },
      images:{
          type: GraphQLList(GraphQLString)
      },
    },
    async resolve(parent, args, { verifiedUser }) {
        if (!verifiedUser) {
            throw new Error("Unauthenticated")
          }
        const updateAd = await Ad.findOneAndUpdate({ _id: args.id, id_user: verifiedUser._id},args)
        if (!updateAd) {
            throw new Error("No Ad with the given ID found for the user")
          }
          return args
    } 
};
