const queryString = require('query-string')
const { assign } = require('lodash')

const authorisedRequest = require('../../lib/authorised-request')
const config = require('../../../config')

const entities = [
  {
    entity: 'company',
    path: 'companies',
    text: 'Companies',
    noun: 'company',
    count: 0,
  },
  {
    entity: 'contact',
    path: 'contacts',
    text: 'Contacts',
    noun: 'contact',
    count: 0,
  },
  {
    entity: 'event',
    path: 'events',
    text: 'Events',
    noun: 'event',
    count: 0,
  },
  {
    entity: 'interaction',
    path: 'interactions',
    text: 'Interactions',
    noun: 'interaction',
    count: 0,
  },
  {
    entity: 'investment_project',
    path: 'investment-projects',
    text: 'Investment projects',
    noun: 'investment project',
    count: 0,
  },
  {
    entity: 'order',
    path: 'omis',
    text: 'Orders',
    noun: 'order',
    count: 0,
  },
]

function search ({ token, searchTerm = '', searchEntity, requestBody, isAggregation = true, limit = 10, page = 1 }) {
  const searchUrl = `${config.apiRoot}/v3/search`
  let options = {
    url: isAggregation ? searchUrl : `${searchUrl}/${searchEntity}`,
    method: isAggregation ? 'GET' : 'POST',
  }
  requestBody = assign({}, requestBody, {
    term: searchTerm,
    limit,
    offset: (page * limit) - limit,
  })

  if (isAggregation) {
    options.qs = assign(requestBody, {
      entity: searchEntity,
    })
  } else {
    options.body = requestBody
  }

  return authorisedRequest(token, options)
    .then(result => {
      result.page = page
      return result
    })
}

// TODO the search endpoints need aligning see Jira DH-293 for details
function searchCompanies ({ token, searchTerm, isUkBased, page = 1, limit = 10 }) {
  const queryParams = {
    offset: (page * limit) - limit,
    limit,
  }
  const body = {
    original_query: searchTerm,
    uk_based: isUkBased,
    isAggregation: false,
  }
  const options = {
    url: `${config.apiRoot}/v3/search/company?${queryString.stringify(queryParams)}`,
    method: 'POST',
    body,
  }

  return authorisedRequest(token, options)
    .then(result => {
      result.page = page

      return result
    })
}

function searchInvestments ({ token, searchTerm, page = 1, limit = 10, filters = {} }) {
  const queryParams = {
    offset: (page * limit) - limit,
    limit,
  }
  const body = assign({}, filters, {
    original_query: searchTerm,
    searchEntity: 'investment_project',
  })

  const options = {
    url: `${config.apiRoot}/v3/search/investment_project?${queryString.stringify(queryParams)}`,
    method: 'POST',
    body,
  }

  return authorisedRequest(token, options)
    .then((result) => {
      result.page = page
      return result
    })
}

function searchForeignCompanies (options) {
  const optionsUkBasedFalse = assign({}, options, { isUkBased: false })

  return searchCompanies(optionsUkBasedFalse)
}

function searchLimitedCompanies (options) {
  const optionsUkBasedTrue = assign({}, options, { isUkBased: true })

  return searchCompanies(optionsUkBasedTrue)
    .then((result) => {
      const limitedCompanies = result.results.filter((company) => {
        return company.company_number ||
          (company.business_type && company.business_type.name.toLowerCase().includes('limited company'))
      })

      return {
        page: result.page,
        results: limitedCompanies,
        count: result.count,
      }
    })
}

module.exports = {
  entities,
  search,
  searchCompanies,
  searchLimitedCompanies,
  searchForeignCompanies,
  searchInvestments,
}

@@ -2952,388 +2952,339 @@
 es (
-options) %7B%0A  const optionsUkBasedTrue = assign(%7B%7D, options, %7B isUkBased: true %7D)%0A%0A  return searchCompanies(optionsUkBasedTrue)%0A    .then((result)
+%7B token, searchTerm, page = 1, limit = 10 %7D) %7B%0A  const queryParams
  =
-%3E
  %7B%0A    
-  const limitedCompanies = result.results.filter((company)
+offset: (page * limit) - limit,%0A    limit,%0A  %7D%0A  const body
  =
-%3E
  %7B%0A    
-    return company.company_number %7C%7C%0A          (company.business_type && company.business_type.name.toLowerCase().includes('limited company'))%0A    
+original_query: searchTerm,%0A  %7D%0A%0A  const options = %7B%0A    url: %60$%7Bconfig.apiRoot%7D/v3/search/companieshousecompany?$%7BqueryString.stringify(queryParams)%7D%60,%0A    method: 'POST',%0A    body,%0A
   %7D
-)
 %0A%0A
-    
   re
@@ -3292,108 +3292,110 @@
 urn 
-%7B%0A        page: result.page,%0A        results: limitedCompanies,%0A        count: result.count,%0A      %7D
+authorisedRequest(token, options)%0A    .then((result) =%3E %7B%0A      result.page = page%0A      return result
 %0A   

Create updated search for ltd company