const apiKey = '8ALsk8jDOlynEwn8ScMBSnG87';

function main() {
  Utilities.sleep(7500);
  var folder = DriveApp.getFolderById("1hAz7O-eoxFUk4YW7FoHr4uNJmPs7CiV5BjBNTLtMJzXwY0CbpykYgSAkFR5Quy_MPdn3e_5j");
  var files = folder.getFiles();
  while (files.hasNext()) {
    var file = files.next();
    var getDate = new Date();
    var date = Utilities.formatDate(getDate, 'America/New_York', 'MM/dd/yyyy hh');
    var fileDate = file.getDateCreated();
    var fileDateFormatted = Utilities.formatDate(fileDate, 'America/New_York', 'MM/dd/yyyy hh');
    if (date == fileDateFormatted) {
      Logger.log("Updated: "+file)
      var updated_file = file.getBlob()
      var updated_blob = updated_file.getDataAsString();
      var updated_data = Utilities.parseCsv(updated_blob, ',');
      var updated_data = updated_data.sort();
    } else if (date != fileDateFormatted) {
      Logger.log("Outdated: "+file)
      var outdated_file = file.getBlob()
      var outdated_blob = outdated_file.getDataAsString();
      var outdated_data = Utilities.parseCsv(outdated_blob, ',');
      var outdated_data = outdated_data.sort();
    }
  }
  austinComparseData(outdated_data, updated_data);
  moveOldFiles();
  }

function moveOldFiles() {
  Logger.log("Moving files.")
  var folder = DriveApp.getFolderById("1hAz7O-eoxFUk4YW7FoHr4uNJmPs7CiV5BjBNTLtMJzXwY0CbpykYgSAkFR5Quy_MPdn3e_5j")
  var files = folder.getFiles()
  while (files.hasNext()) {
      var file = files.next();
      var getDate = new Date();
      var date = Utilities.formatDate(getDate, 'America/New_York', 'MM/dd/yyyy hh' );
      var fileDate = file.getDateCreated();
       Logger.log("FILE: "+file+", DATE UPLOADED: "+fileDate)
      var fileDateFormatted = Utilities.formatDate(fileDate, 'America/New_York', 'MM/dd/yyyy hh');
      if (date != fileDateFormatted) {
        const destination = DriveApp.getFolderById('1cIPZCgoj8XdjMIfL75QfomJAxsy2smGT');
        file.moveTo(destination);
    }
  }
}

function austinComparseData(outdated_data, updated_data) {
  let domains_to_update = [];
  let updatedFlag = 1;
  let outdatedFlag = 1;

  while(updatedFlag < updated_data.length && outdatedFlag < outdated_data.length){
    if(updated_data[updatedFlag][0] == outdated_data[outdatedFlag][0]) {
      let allUpdatedGroups = [];
      let allOutdatedGroups = [];

      for(let i = 1; i < updated_data[updatedFlag].length; i++){
        if(updated_data[updatedFlag][i]){
          allUpdatedGroups.push(updated_data[updatedFlag][i].trim());
        }
      }
      for(let j = 1; j < outdated_data[outdatedFlag].length; j++){
        if(outdated_data[outdatedFlag][j]){
          allOutdatedGroups.push(outdated_data[outdatedFlag][j].trim());
        }
      }

      if(allUpdatedGroups.length !== allOutdatedGroups.length){
        domains_to_update.push(updated_data[updatedFlag]);
      } else {
        for(let k = 0; k < allUpdatedGroups.length; k++){
          if(allOutdatedGroups.indexOf(allUpdatedGroups[k]) == -1){
            domains_to_update.push(updated_data[updatedFlag]);
          }
        }
      }

      updatedFlag++;
      outdatedFlag++;
    } else {
      if(updated_data[updatedFlag][0] > outdated_data[outdatedFlag][0]){
        outdatedFlag++;
      }
      else if(updated_data[updatedFlag][0] < outdated_data[outdatedFlag][0]){
        domains_to_update.push(updated_data[updatedFlag]);
        updatedFlag++;
      }
    }

    if(updatedFlag > updated_data.length-1 || outdatedFlag > outdated_data.length-1){
      break;
    }
  }
  updatedDomainsOnly(domains_to_update);
}

function updatedDomainsOnly(domains_to_update) {
  var errorList = [];
  var corrList = [];
  for (var x = 0; x < domains_to_update.length; x++) {
    var array = domains_to_update[x];
    Logger.log(array)
    var currDomain = domains_to_update[x][0]
    var errorObj = new Object();
    var noErrorObj = new Object();
    Logger.log("Current Domain: "+currDomain);
    var errorGroups = []
    var correctGroups = []
    for (var i = 1; i < array.length; i++) {
      var item = array[i]
      if (item != "") {
        var api_url = 'https://api.northpass.com/v2/groups/?filter[name][eq]='+encodeURIComponent(item);
        const settings = {
        async: true,
        crossDomain: true,
        method: 'GET',
        headers: {
                accept: 'application/json',
                'X-Api-Key': apiKey
          }
        };
        const sendMsg = UrlFetchApp.fetch(api_url, settings);
        var uuidResponse = sendMsg.getContentText();
        var parseData = JSON.parse(uuidResponse);
        var groupName = array.indexOf(item);
        if (parseData['data'].length >= 1) {
          var groupID = parseData['data'][0]['id'];
          if (groupName != -1) {
              array[groupName] = groupID;
          } 
          correctGroups.push(item)
        } else {
          errorGroups.push(item);
          array[groupName] = "";
        }
      }
    }
    errorObj[currDomain] = errorGroups;
    noErrorObj[currDomain] = correctGroups;
    corrList.push(noErrorObj)
    errorList.push(errorObj)

    const dom = array.shift();
    const groups = array;
    Logger.log("{domain_to_update : {domain :"+dom+", group_ids : "+groups+" }}")
    sendWebhook({domain_to_update : { domain: dom, group_ids: groups }})
    replaceOnSheet(dom, groups);
    }
    MailApp.sendEmail("amitd@anthology.com",
                    "Anthology has submitted a CSV",
                    "Here are the domains that will be updated: "+JSON.stringify(noErrorObj)+"\n\n"+"Group Errors (they likely don't exist): "+JSON.stringify(errorList));
}

function replaceOnSheet(dom, groups) {
  var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1Kck1UUOkVIU4kbBC8b_bl77fNaCTSFStiv0I5Gg-kIc/');
  var sheet = ss.getActiveSheet()
  var dataRow = Math.max(sheet.getLastRow(),1);
  // let domain = tmpArray.shift();
  // let groupIds = tmpArray;


  let boo = sheet.createTextFinder(dom).findNext()
  if (boo != null ) {
    Logger.log("Replace on Sheet Domain: "+dom)
    Logger.log("New groups: "+groups)
    group2 = []
    currCell = boo.getRowIndex();
    groups.forEach(item => { if (item.length > 0) { group2.push(item) }});
    group2 = [group2];
    Logger.log(group2);
    sheet.getRange(currCell, 1, 1, 100).clear();
    sheet.getRange(currCell, 1).setValue(dom);
    sheet.getRange(currCell, 2, 1, group2[0].length).setValues(group2)
  } else {
    //Logger.log("Add to Sheet: "+dom)
    //Logger.log("else groups: "+groups)
    group2 = []
    //groups.forEach(item => { if (item.includes("")){ } else { group2.push(item) }});
    groups.forEach(item => { if (item.length > 0) { group2.push(item) }});
    //Logger.log(group2);
    group2 = [group2];
    sheet.getRange(dataRow+1, 1).setValue(dom);
    sheet.getRange(dataRow+1, 2, 1, group2[0].length).setValues(group2)
  }
}

function sendWebhook(domain_to_update){
  //Logger.log("Webhook function Object: "+JSON.stringify(domain_to_update))
  var payload = domain_to_update
  var options = {
      'method': 'post',
      'payload': JSON.stringify(payload)
  };
  UrlFetchApp.fetch('https://webhooks.workato.com/webhooks/rest/604b4fc0-fcd9-4f3b-b715-b42d7f740ba6/domains-updated', options);
}
