process.env.NODE_ENV = 'test';

const expect = require('chai').expect;
const request = require('supertest');

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

const ads = require("../controllers/adController")
const users = require("../controllers/userController")

const User = db.users
const Ad = db.ads


describe('Database', function () {

  before(function (done) {
    User.deleteMany({}, () => {
      Ad.deleteMany({}, () => {
        done()
      })
    })
  })



  it('Create a new user ', (done) => {
    request(app).post('/graphql')
      .set('content-type', 'application/json')
      .set('Accept', 'application/json')
      .set({ "Authorization": `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7Il9pZCI6IjYxODMxMDkwNzE5MDU5N2QyYTMxYmI3NSIsInVzZXJuYW1lIjoiaW1pbmUiLCJlbWFpbCI6ImNoYW91cmFyLmltaW5lQGdtYWlsLmNvbSIsInBhc3N3b3JkIjoiJDJiJDEwJExVd0suQ2drUkREdEQ1dHFVYkJWU08wcFVnMnlkTVRNbWUybTQySnd6NnZuUFJsbUxHejl1IiwiX192IjowfSwiaWF0IjoxNjM4NTYwMDAwLCJleHAiOjE2NDExNTIwMDB9.CkWGypJdnyKDz5sZBESn9UxR7KET-Dz9t0UQ8YRBNqs` })
      .send({ query: "mutation{register(username :\"imine\", email: \"chaourar.imine@gmail.com\", password:\"12345\"){email}}" })
      .then((res) => {
        const a = User.find({}).countDocuments(function (err, count) {
          expect(count).to.equal(1);
          done()
        });
      })
      .catch((err) => done(err));
  });


  it('Create a new user with an existing amail ', (done) => {

    request(app).post('/graphql')
      .set('content-type', 'application/json')
      .set('Accept', 'application/json')
      .set({ "Authorization": `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7Il9pZCI6IjYxODMxMDkwNzE5MDU5N2QyYTMxYmI3NSIsInVzZXJuYW1lIjoiaW1pbmUiLCJlbWFpbCI6ImNoYW91cmFyLmltaW5lQGdtYWlsLmNvbSIsInBhc3N3b3JkIjoiJDJiJDEwJExVd0suQ2drUkREdEQ1dHFVYkJWU08wcFVnMnlkTVRNbWUybTQySnd6NnZuUFJsbUxHejl1IiwiX192IjowfSwiaWF0IjoxNjM4NTYwMDAwLCJleHAiOjE2NDExNTIwMDB9.CkWGypJdnyKDz5sZBESn9UxR7KET-Dz9t0UQ8YRBNqs` })
      .send({
        query: `mutation{
        register(
          username :"imine", 
          email: "chaourar.imine@gmail.com", 
          password:"12345"){
            email
          }
        }`})
      .then((res) => {
        const a = User.find({}).countDocuments(function (err, count) {
          expect(count).to.equal(1);
          done()
        });
      })
      .catch((err) => done(err));
  });



  it('Create a new Ad without a title ', (done) => {

    request(app).post('/graphql')
      .set('content-type', 'application/json')
      .set('Accept', 'application/json')
      .set({ "Authorization": `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7Il9pZCI6IjYxODMxMDkwNzE5MDU5N2QyYTMxYmI3NSIsInVzZXJuYW1lIjoiaW1pbmUiLCJlbWFpbCI6ImNoYW91cmFyLmltaW5lQGdtYWlsLmNvbSIsInBhc3N3b3JkIjoiJDJiJDEwJExVd0suQ2drUkREdEQ1dHFVYkJWU08wcFVnMnlkTVRNbWUybTQySnd6NnZuUFJsbUxHejl1IiwiX192IjowfSwiaWF0IjoxNjM4NTYwMDAwLCJleHAiOjE2NDExNTIwMDB9.CkWGypJdnyKDz5sZBESn9UxR7KET-Dz9t0UQ8YRBNqs` })
      .send({
        query: `mutation{ 
      adCreate(
        type:Vente,
        statusPublication:Publiee,
        statusBien:Disponible,
        description:"Maison T5, R+1, Lardenne, à proximité de toutes commodités (rocade arc en ciel, bus 3min, écoles 2 min, commerces…) , charges annuelles environs 250€",
        prix:420000,
        date:"2021-12-01T00:00:00.000Z",
        images:
                    [
              "16361260117424e4c49e36b91fc0b66b396508b07631581b23259.jpg",
              "163612601174507bb6a240cee6a41790c7a84054810b1a1a2fcde.jpg",
              "1636126011749f49b6e0e920b4b3ac178c0396cfc0c5c137ed88f.jpg"
              ]
          ){
          titre,
            slug,
            type,
            statusPublication,
            statusBien,
            description,
            prix,
            date,
            images
          }
    }`

      })
      .then((res) => {
        const a = Ad.find({}).countDocuments(function (err, count) {
          expect(count).to.equal(0);
          done()
        });
      })
      .catch((err) => done(err));
  });



  it('Create a new Ad ', (done) => {

    request(app).post('/graphql')
      .set('content-type', 'application/json')
      .set('Accept', 'application/json')
      .set({ "Authorization": `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7Il9pZCI6IjYxODMxMDkwNzE5MDU5N2QyYTMxYmI3NSIsInVzZXJuYW1lIjoiaW1pbmUiLCJlbWFpbCI6ImNoYW91cmFyLmltaW5lQGdtYWlsLmNvbSIsInBhc3N3b3JkIjoiJDJiJDEwJExVd0suQ2drUkREdEQ1dHFVYkJWU08wcFVnMnlkTVRNbWUybTQySnd6NnZuUFJsbUxHejl1IiwiX192IjowfSwiaWF0IjoxNjM4NTYwMDAwLCJleHAiOjE2NDExNTIwMDB9.CkWGypJdnyKDz5sZBESn9UxR7KET-Dz9t0UQ8YRBNqs` })
      .send({
        query: `mutation{ 
        adCreate(               
          titre:"Maison Lardenne 125m² ok",
          slug:"Maison-Lardenne-125m",
          type:Vente,
          statusPublication:Publiee,
          statusBien:Disponible,
          description:"Maison T5, R+1, Lardenne, à proximité de toutes commodités (rocade arc en ciel, bus 3min, écoles 2 min, commerces…) , charges annuelles environs 250€",
          prix:420000,
          date:"2021-12-01T00:00:00.000Z",
          images:
                      [
                "16361260117424e4c49e36b91fc0b66b396508b07631581b23259.jpg",
                "163612601174507bb6a240cee6a41790c7a84054810b1a1a2fcde.jpg",
                "1636126011749f49b6e0e920b4b3ac178c0396cfc0c5c137ed88f.jpg"
                ]
            ){
            titre,
              slug,
              type,
              statusPublication,
              statusBien,
              description,
              prix,
              date,
              images
            }
      }`

      })
      .then((res) => {
        const a = Ad.find({}).countDocuments(function (err, count) {
          expect(count).to.equal(1);
          done()
        });
      })
      .catch((err) => done(err));
  });



  it('Update an Ad ', async () => {
    //get the ad

    let ad = await Ad.findOne({ prix: 420000 }).exec()

    newTitle = "New Title :245$"

    let id = ad.id

    request(app).post('/graphql')
      .set('content-type', 'application/json')
      .set('Accept', 'application/json')
      .set({ "Authorization": `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7Il9pZCI6IjYxODMxMDkwNzE5MDU5N2QyYTMxYmI3NSIsInVzZXJuYW1lIjoiaW1pbmUiLCJlbWFpbCI6ImNoYW91cmFyLmltaW5lQGdtYWlsLmNvbSIsInBhc3N3b3JkIjoiJDJiJDEwJExVd0suQ2drUkREdEQ1dHFVYkJWU08wcFVnMnlkTVRNbWUybTQySnd6NnZuUFJsbUxHejl1IiwiX192IjowfSwiaWF0IjoxNjM4NTYwMDAwLCJleHAiOjE2NDExNTIwMDB9.CkWGypJdnyKDz5sZBESn9UxR7KET-Dz9t0UQ8YRBNqs` })
      .send({
        query: "mutation{\
      adUpdate(\
        id:\""+ id + "\"\
        , titre:\"New Title :245$\"\
          ){\
          titre,\
          }\
        }"
      })
      .then(async (res) => {
        let ad = await Ad.findOne({ prix: 420000 }).exec()
        expect(ad.titre).to.equal(newTitle);
        await new Promise(resolve => resolve());
      })
      .catch((err) => done(err));
  });


  it('Update an Ad without submiting its id', async () => {
    //get the ad

    let ad = await Ad.findOne({ prix: 420000 }).exec()

    newTitle = "New Title :245$"

    let id = ad.id

    request(app).post('/graphql')
      .set('content-type', 'application/json')
      .set('Accept', 'application/json')
      .set({ "Authorization": `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7Il9pZCI6IjYxODMxMDkwNzE5MDU5N2QyYTMxYmI3NSIsInVzZXJuYW1lIjoiaW1pbmUiLCJlbWFpbCI6ImNoYW91cmFyLmltaW5lQGdtYWlsLmNvbSIsInBhc3N3b3JkIjoiJDJiJDEwJExVd0suQ2drUkREdEQ1dHFVYkJWU08wcFVnMnlkTVRNbWUybTQySnd6NnZuUFJsbUxHejl1IiwiX192IjowfSwiaWF0IjoxNjM4NTYwMDAwLCJleHAiOjE2NDExNTIwMDB9.CkWGypJdnyKDz5sZBESn9UxR7KET-Dz9t0UQ8YRBNqs` })
      .send({
        query: "mutation{\
      adUpdate(\
        , titre:\"New Title :245$\"\
          ){\
          titre,\
          }\
        }"
      })
      .then(async (res) => {
        let ad = await Ad.findOne({ prix: 420000 }).exec()
        expect(ad.titre).to.not.equal(newTitle);
        await new Promise(resolve => resolve());
      })
      .catch((err) => done(err));
  });


  it('Ask a question ', async () => {
    //get the ad

    let ad = await Ad.findOne({ prix: 420000 }).exec()
    let id = ad.id

    request(app).post('/graphql')
      .set('content-type', 'application/json')
      .set('Accept', 'application/json')
      .set({ "Authorization": `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7Il9pZCI6IjYxODMxMDkwNzE5MDU5N2QyYTMxYmI3NSIsInVzZXJuYW1lIjoiaW1pbmUiLCJlbWFpbCI6ImNoYW91cmFyLmltaW5lQGdtYWlsLmNvbSIsInBhc3N3b3JkIjoiJDJiJDEwJExVd0suQ2drUkREdEQ1dHFVYkJWU08wcFVnMnlkTVRNbWUybTQySnd6NnZuUFJsbUxHejl1IiwiX192IjowfSwiaWF0IjoxNjM4NTYwMDAwLCJleHAiOjE2NDExNTIwMDB9.CkWGypJdnyKDz5sZBESn9UxR7KET-Dz9t0UQ8YRBNqs` })
      .send({
        query: "mutation{\
      ask(\
        ad_id:\""+ id + "\"\
        ,username:\"Chaourar IMINE \"\
        ,question:\"For how many days? \"\
          ){\
          titre,\
          }\
        }"
      })
      .then(async (res) => {
        let ad = await Ad.findOne({ prix: 420000 }).exec()
        const a = ad.questions.length
        expect(a).to.equal(1);
        await new Promise(resolve => resolve());
      })
      .catch((err) => done(err));
  });



  it('Delete an Ad ', async () => {
    //get the ad

    let ad = await Ad.findOne({ prix: 420000 }).exec()


    let id = ad.id

    request(app).post('/graphql')
      .set('content-type', 'application/json')
      .set('Accept', 'application/json')
      .set({ "Authorization": `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7Il9pZCI6IjYxODMxMDkwNzE5MDU5N2QyYTMxYmI3NSIsInVzZXJuYW1lIjoiaW1pbmUiLCJlbWFpbCI6ImNoYW91cmFyLmltaW5lQGdtYWlsLmNvbSIsInBhc3N3b3JkIjoiJDJiJDEwJExVd0suQ2drUkREdEQ1dHFVYkJWU08wcFVnMnlkTVRNbWUybTQySnd6NnZuUFJsbUxHejl1IiwiX192IjowfSwiaWF0IjoxNjM4NTYwMDAwLCJleHAiOjE2NDExNTIwMDB9.CkWGypJdnyKDz5sZBESn9UxR7KET-Dz9t0UQ8YRBNqs` })
      .send({
        query: "mutation{\
      adDelete(\
        id:\""+ id + "\"\
          ){\
          titre,\
          }\
        }"
      })
      .then(async (res) => {
        const a = await Ad.countDocuments();
        expect(a).to.equal(0);
        await new Promise(resolve => resolve());
      })
      .catch((err) => done(err));
  });



});












// // var assert = require('assert');
// // describe('Array', function() {
// //   describe('#indexOf()', function() {
// //     it('should return -1 when the value is not present', function() {
// //       assert.equal([1, 2, 3].indexOf(4), -1);
// //     });
// //   });
// // });
