import {getFieldIndexMap, batchGetValues, appendRows} from './sheets.js'
import {formatAsHyperlink, formatDate, mapFieldsToRow} from './utils'
import {sheets, NEW_ORDER_STATUS} from './constants'
import c from '../config'

export async function storeOrder(variant, spreadsheetId, order, items) {
  const sheet = order.isCompany ? sheets[variant].companyOrders : sheets[variant].personalOrders

  const fieldIndexMap = await getFieldIndexMap(spreadsheetId, sheet.name, sheet.fieldsRow)

  const user = order.user
  const userJiraId = await getUserJiraId(variant, spreadsheetId, user.id, user.name)

  const date = new Date()

  const itemToSheetData = order.isCompany
    ? mapCompanyOrderItemToSheetData
    : mapPersonalOrderItemToSheetData

  const data = []

  for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
    const item = items[itemIndex]

    for (let i = 0; i < item.count; i++) {
      data.push(
        mapFieldsToRow(
          fieldIndexMap,
          itemToSheetData(
            `${item.dbIds[i]}${c.google.orderIdSuffix}`,
            item,
            order,
            userJiraId,
            date,
          ),
        ),
      )
    }
  }

  return appendRows(spreadsheetId, sheet.name, data)
}

async function getUserJiraId(variant, spreadsheetId, slackId, name) {
  const sheetData = await batchGetValues(spreadsheetId, [
    sheets[variant].settings.jiraIdRange,
    sheets[variant].settings.slackIdRange,
  ])
  const values = sheetData.map((vr) => vr.values)

  const jiraIds = values[0]
  const slackIds = values[1]
  const rowIndex = slackIds.findIndex((i) => {
    return i.length === 1 && i[0] === slackId
  })

  if (rowIndex === -1) {
    return `${name}:${slackId}`
  }

  return jiraIds[rowIndex][0]
}

function mapPersonalOrderItemToSheetData(
  dbId,
  item,
  order,
  userJiraId,
  date,
) {
  return {
    'UUID': dbId,
    'Name': formatAsHyperlink(item.url, item.name),
    'Office': order.office,
    'Reason': order.reason,
    'Urgent': Boolean(order.isUrgent),
    'Status': NEW_ORDER_STATUS,
    'User ID': userJiraId,
    'ts': formatDate(date),
  }
}

function mapCompanyOrderItemToSheetData(
  dbId,
  item,
  order,
  userJiraId,
  date,
) {
  return {
    'UUID': dbId,
    'Name': formatAsHyperlink(item.url, item.name),
    'Office': order.office,
    'Reason': order.reason,
    'Status': NEW_ORDER_STATUS,
    'Requested by': userJiraId,
    'ts': formatDate(date),
  }
}

@@ -622,126 +622,8 @@
 ()%0A%0A
-  const itemToSheetData = order.isCompany%0A    ? mapCompanyOrderItemToSheetData%0A    : mapPersonalOrderItemToSheetData%0A%0A
   co
@@ -875,16 +875,37 @@
 etData(%0A
+            variant,%0A
         
@@ -1664,25 +1664,9 @@
 ion 
-mapPersonalOrderI
+i
 temT
@@ -1669,32 +1669,43 @@
 temToSheetData(%0A
+  variant,%0A
   dbId,%0A  item,%0A
@@ -1745,194 +1745,41 @@
 %7B%0A  
-return %7B%0A    'UUID': dbId,%0A    'Name': formatAsHyperlink(item.url, item.name),%0A    'Office': order.office,%0A    'Reason': order.reason,%0A    'Urgent': Boolean(order.isUrgent),
+const commonFields = %7B
 %0A    
-'
 Status
-'
 : NE
@@ -1802,39 +1802,10 @@
     
-'User ID': userJiraId,%0A    '
 ts
-'
 : fo
@@ -1828,124 +1828,147 @@
   %7D%0A
-%7D%0A%0Afunction mapCompanyOrderItemToSheetData(%0A  dbId,%0A  item,%0A  order,%0A  userJiraId,%0A  date,%0A) %7B%0A  return
+  const variantFields = variant === 'wincent' ? %7B%0A    ID: dbId,%0A    Specification: formatAsHyperlink(item.url, item.name),%0A  %7D :
  %7B%0A    
-'
 UUID
-'
 : db
@@ -1979,14 +1979,12 @@
     
-'
 Name
-'
 : fo
@@ -2029,16 +2029,14 @@
     
-'
 Office
-'
 : or
@@ -2053,124 +2053,313 @@
 ,%0A  
-  'Reason': order.reason,%0A    'Status': NEW_ORDER_STATUS,%0A    'Requested by': userJiraId,%0A    'ts': formatDate(date)
+%7D%0A  const companyOrPersonalFields = order.isCompany ? %7B%0A    'Reason': order.reason,%0A    'Urgent': Boolean(order.isUrgent),%0A    'User ID': userJiraId,%0A  %7D : %7B%0A    'Note': order.reason,%0A    'Requested by': userJiraId,%0A  %7D%0A%0A  return %7B%0A    ...commonFields,%0A    ...variantFields,%0A    ...companyOrPersonalFields
 ,%0A  

split sheets fields by variant + slight refactor