import {getValues, batchGetValues, batchUpdateValues} from './sheets.js'
import {formatAsHyperlink, formatDate} from './utils'
import {sheets} from './constants'

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

  const newRowIndex = await getNextEmptyRowIndex(sheet)

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

  const date = new Date()

  const itemToSheetData = order.isCompany
    ? mapCompanyOrderItemToSheetData
    : mapPersonalOrderItemToSheetData

  const data = items.map((item, index) => {
    return itemToSheetData(
      item,
      newRowIndex + index,
      order,
      sheet.name,
      userJiraId,
      date,
    )
  })

  await batchUpdateValues(sheet, {data, valueInputOption: 'USER_ENTERED'})
}

async function getNextEmptyRowIndex(sheet) {
  const values = await getValues(sheet.idRange)
  let firstEmpty = values.findIndex((v) => v.every((c) => !c))

  if (firstEmpty === -1) {
    firstEmpty = values.length
  }

  return firstEmpty + sheet.rowOffset + 1
}

async function getUserJiraId(slackId, name) {
  const sheetData = await batchGetValues([
    sheets.settings.jiraIdRange,
    sheets.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(
  item,
  rowIndex,
  order,
  sheetName,
  userJiraId,
  date,
) {
  return [
    {
      range: `${sheetName}!A${rowIndex}:J${rowIndex}`,
      values: [
        [
          item.dbId,
          formatAsHyperlink(item.url, item.name),
          item.price,
          order.office,
          'requested',
          null,
          null,
          userJiraId,
          null,
          formatDate(date),
        ],
      ],
    },
  ]
}

function mapCompanyOrderItemToSheetData(
  item,
  rowIndex,
  order,
  sheetName,
  userJiraId,
  date,
) {
  return [
    {
      range: `${sheetName}!A${rowIndex}:M${rowIndex}`,
      values: [
        [
          item.dbId,
          formatAsHyperlink(item.url, item.name),
          item.price,
          null,
          null,
          order.office,
          order.reason,
          'requested',
          null,
          null,
          userJiraId,
          null,
          formatDate(date),
        ],
      ],
    },
  ]
}

@@ -1732,17 +1732,17 @@
 wIndex%7D:
-J
+L
 $%7BrowInd
@@ -1858,32 +1858,48 @@
     item.price,%0A
+          null,%0A
           order.
@@ -1953,32 +1953,48 @@
           null,%0A
+          null,%0A
           userJi
@@ -2247,9 +2247,9 @@
 ex%7D:
-M
+N
 $%7Bro
@@ -2457,16 +2457,16 @@
 reason,%0A
-
         
@@ -2504,32 +2504,48 @@
           null,%0A
+          null,%0A
           userJi

Fix sheet column indexes