
const chai = require('chai')
chai.use(require('chai-http'))

const expect = chai.expect

const constants = require('./constants.js')
const app = require('../../src/index.js')

// Helper function to reserve a CVE-ID
async function cveIdReserveHelper (requestLength, year, shortName, batchType) {
  return await chai.request(app)
    .post(`/api/cve-id?amount=${requestLength}&cve_year=${year}&short_name=${shortName}&batch_type=${batchType}`)
    .set(constants.nonSecretariatUserHeaders)
    .then((res, err) => {
      return res.body.cve_ids[0].cve_id
    })
}

// Helper function to bulk reserve CVE-ID
async function cveIdBulkReserveHelper (requestLength, year, shortName, batchType) {
  return await chai.request(app)
    .post(`/api/cve-id?amount=${requestLength}&cve_year=${year}&short_name=${shortName}&batch_type=${batchType}`)
    .set(constants.headers)
    .then((res, err) => {
      const ids = res.body.cve_ids.map(cveId => {
        return cveId.cve_id
      })
      return ids
    })
}

// Helper function to publish a CVE
async function cveRequestAsCnaHelper (cveId) {
  await chai.request(app)
    .post(`/api/cve/${cveId}/cna`)
    .set(constants.nonSecretariatUserHeaders)
    .send(constants.testCve)
    .then((res, err) => {
      // Safety Expect
      expect(res).to.have.status(200)
    })
}

async function cveRequestAsSecHelper (cveId) {
  await chai.request(app)
    .post(`/api/cve/${cveId}/cna`)
    .set(constants.headers)
    .send(constants.testCve)
    .then((res, err) => {
      // Safety Expect
      expect(res).to.have.status(200)
    })
}

async function cveRequestAsCnaHelperWithCnaContainer (cveId, cnaContainer) {
  await chai.request(app)
    .post(`/api/cve/${cveId}/cna`)
    .set(constants.nonSecretariatUserHeaders)
    .send(cnaContainer)
    .then((res, err) => {
      // Safety Expect
      expect(res).to.have.status(200)
    })
}

async function cveUpdatetAsCnaHelperWithCnaContainer (cveId, cnaContainer) {
  await chai.request(app)
    .put(`/api/cve/${cveId}/cna`)
    .set(constants.nonSecretariatUserHeaders)
    .send(cnaContainer)
    .then((res, err) => {
      // Safety Expect
      expect(res).to.have.status(200)
    })
}

async function cveUpdateAsSecHelper (cveId, cnaContainer) {
  await chai.request(app)
    .put(`/api/cve/${cveId}/cna`)
    .set(constants.headers)
    .send(cnaContainer)
    .then((res, err) => {
      // Safety Expect
      expect(res).to.have.status(200)
    })
}

async function cveUpdateAsCnaHelperWithAdpContainer (cveId, adpContainer) {
  await chai.request(app)
    .put(`/api/cve/${cveId}/adp`)
    .set(constants.nonSecretariatUserHeaders)
    .send(adpContainer)
    .then((res, err) => {
      expect(res).to.have.status(200)
    })
}

module.exports = {
  cveIdReserveHelper,
  cveIdBulkReserveHelper,
  cveRequestAsCnaHelper,
  cveRequestAsCnaHelperWithCnaContainer,
  cveRequestAsSecHelper,
  cveUpdatetAsCnaHelperWithCnaContainer,
  cveUpdateAsSecHelper,
  cveUpdateAsCnaHelperWithAdpContainer
}
