'use strict'

const satlib = require('@sat-utils/api-lib')
const _ = require('lodash')

/* Example Handler
{
  'satellite': 'landsat',
  'arn': *arn of step function to do transform*,


}
*/
module.exports.handler = function handler(event, context, cb) {
  console.log('ingest event: ', JSON.stringify(event))
  const sat = event.satellite
  const bucket = process.env.bucket || 'sat-api'
  let key = process.env.prefix || 'sat-api-dev'
  key = `${key}/ingest/${sat}/`
  const maxFiles = _.get(event, 'maxFiles', 0)
  const linesPerFile = _.get(event, 'linesPerFile', 500)
  const maxLambdas = _.get(event, 'maxLambdas', 30)
  const arn = _.get(event, 'arn', '')

  let url
  let reverse = true
  switch (sat) {
  case 'landsat':
    url = 'https://landsat.usgs.gov/landsat/metadata_service/bulk_metadata_files/LANDSAT_8_C1.csv'
    reverse = false
    break
  case 'sentinel':
    url = 'https://storage.googleapis.com/gcp-public-data-sentinel-2/index.csv.gz'
    break
  default:
    return cb('no match was found for satellite')
  }

  return satlib.ingestcsv.split({
    url,
    bucket,
    key,
    arn,
    inMaxFiles: maxFiles,
    linesPerFile,
    maxLambdas,
    reverse,
    cb
  })
}


@@ -56,678 +56,260 @@
 b')%0A
-const _ = require('lodash')%0A%0A/* Example Handler%0A%7B%0A  'satellite': 'landsat',%0A  'arn': *arn of step function to do transform*,%0A%0A%0A%7D%0A*/%0Amodule.exports.handler = function handler(event, context, cb) %7B%0A  console.log('ingest event: ', JSON.stringify(event))%0A  const sat = event.satellite%0A  const bucket = process.env.bucket %7C%7C 'sat-api'%0A  let key = process.env.prefix %7C%7C 'sat-api-dev'%0A  key = %60$%7Bkey%7D/ingest/$%7Bsat%7D/%60%0A  const maxFiles = _.get(event, 'maxFiles', 0)%0A  const linesPerFile = _.get(event, 'linesPerFile', 500)%0A  const maxLambdas = _.get(event, 'maxLambdas', 30)%0A  const arn = _.get(event, 'arn', '')%0A%0A  let url%0A  let reverse = true%0A  switch (sat) %7B%0A  case 'landsat':
+%0A%0A// SNS message%0Amodule.exports.handler = function handler(event, context, cb) %7B%0A  console.log('ingest event: ', JSON.stringify(event))%0A  const msg = JSON.parse(event.Records%5B0%5D.Sns.Message)%0A  console.log(msg)%0A  let url%0A  msg.Records.forEach((val) =%3E %7B
 %0A   
@@ -315,25 +315,25 @@
   url = 
-'
+%60
 https://
 landsat.
@@ -328,453 +328,139 @@
 s://
-landsat.usgs.gov/landsat/metadata_service/bulk_metadata_files/LANDSAT_8_C1.csv'%0A    reverse = false%0A    break%0A  case 'sentinel':%0A    url = 'https://storage.googleapis.com/gcp-public-data-sentinel-2/index.csv.gz'%0A    break%0A  default:%0A    return cb('no match was found for satellite')%0A  %7D%0A%0A  return satlib.ingestcsv.split(%7B%0A    url,%0A    bucket,%0A    key,%0A    arn,%0A    inMaxFiles: maxFiles,%0A    linesPerFile,%0A    maxLambdas,%0A    reverse,%0A    cb
+$%7Bval.s3.bucket.name%7D.s3.amazonaws.com/$%7Bval.s3.object.key%7D%60%0A    console.log(%60ingesting $%7Burl%7D%60)%0A    satlib.ingest.ingest(url)
 %0A  %7D)%0A%7D%0A
 %0A
@@ -458,9 +458,8 @@
 %0A  %7D)%0A%7D%0A
-%0A

change ingest lambda to SNS ingest