const csvToJson = require('csvtojson');
const { Parser } = require('json2csv');
const fs = require('fs');

function formatPhoneNumber(phoneNumber){
  if (phoneNumber.length > 0){
    return `(${phoneNumber.slice(0, 3)}) ${phoneNumber.slice(3, 6)}-${phoneNumber.slice(6)}`;
  }
  return phoneNumber;
}

function addQuotes(value){
  if (value.includes(',')){
    return `"${value}"`;
  }
  return value;
}

function addCommaToNumber(value){
  for(var i = value.length; i > 0; i = i - 3){
    value = `${value.slice(0, i)},${value.slice(i)}`;
  }
  return value.slice(0, value.length - 1);
}

function removeExtraZipCode(value){
  var pieces = value.split('-');
  return pieces[0];
}

function processOwnerFile(){
  csvToJson({
    noheader: false,
    headers: [
      'PropertyAddress',
      'PropertyName',
      'RBA',
      'City',
      'State',
      'Zip',
      'CountyName',
      'LandAreaAC',
      'ParcelNumber1',
      'PropertyType',
      'TrueOwnerAddress',
      'TrueOwnerCityStateZip',
      'TrueOwnerContact',
      'TrueOwnerName',
      'TrueOwnerPhone',
      'SecondaryType',
      'OwnerName',
      'OwnerPhone'
    ]
  }).fromFile('./data/i96.csv').then((jsonObj) => {
    let data = jsonObj.map((x) => {
      let y = {};
      y['Description Address L Name'] = `${x.PropertyAddress} ${x.City} ${x.State} ${x.Zip.slice(0, 5)}`;
      y['Property Address L Custom Building Address'] = `${x.PropertyAddress} ${x.City} ${x.State} ${x.Zip.slice(0, 5)}`;
      y['Property Name L Custom Property Name'] = addQuotes(x.PropertyName);
      y['RBA L Custom SF Building'] = addQuotes(addCommaToNumber(x.RBA));
      y['Property Address P'] = x.PropertyAddress;
      y['City P'] = x.City;
      y['State P'] = x.State;
      y['No Zip'] = x.Zip;
      y['Zip P'] = x.Zip.slice(0, 5);
      y['County Name C Territory'] = `${x.CountyName} County`;
      y['Land Area (AC) L Custom'] = x.LandAreaAC;
      y['Parcel Number L Custom'] = x.ParcelNumber1;
      y['Property Type L Tag'] = 'Industrial';
      y['True Owner Address C Address: Main Address 1 Full Adress'] = addQuotes(x.TrueOwnerAddress);
      y['True Owner City State Zip C Address: Main City'] = x.TrueOwnerCityStateZip.length > 0 ? addQuotes(`${removeExtraZipCode(x.TrueOwnerCityStateZip)}`): x.TrueOwnerCityStateZip;
      y['True Owner Contact P Full Name'] = addQuotes(x.TrueOwnerContact);
      y['True Owner Name L Custom True Owner'] = addQuotes(x.TrueOwnerName);
      y['True Owner Phone P Work'] = formatPhoneNumber(x.TrueOwnerPhone);
      y['Secondary Type L Custom'] =  addQuotes(x.SecondaryType);
      y['Owner Name L Custom Owners Name'] = '';
      y['Owner Phone L owner Phone'] = '';
      y['Owner Name Name C Name'] = x.TrueOwnerName.length > 0 ? addQuotes(x.TrueOwnerName): addQuotes(x.OwnerName);
      y['Assignee L Assignee'] = 'EJR';
      y['C Tag Industrial'] = 'Industrial';
      y['Market Area L Custom'] = 'Central I-96 Corridor';
      y['Owner C Tag'] = 'Owner';
      y['Status L'] = 'Pending';
      y['C Description'] = 'Owner';

      return y;
    });
    fs.writeFileSync('./data/result.json', JSON.stringify(data, null, 2));
    const parser = new Parser({
      quote: '',
    });
    const csv = parser.parse(data);
    // console.log(csv);
    fs.writeFileSync('./data/result.csv', csv);
  });
}

function processTenantFile(){
  csvToJson({
    noheader: false,
    headers: [
      'FullName',            
      'LastName',
      'FirstName',           
      'Title',
      'CompanyName',         
      'Phone',
      'Address',             
      'Suite',
      'City',                
      'State',
      'Zip',                 
      'TenantWebAddress',
      'Zip4',               
      'TenantSICDescription',
      'TenantSFOccupied',   
      'TenantLocation',
      'TenantEmp',         
      'TenPhone',
      'MoveDate',            
      'MailingAddressAddress',
      'MailingAddressCity', 
      'MailingAddressState',
      'MailingAddressZip'
    ]
  }).fromFile('./data/i96tenant.csv').then(jsonObj => {
    let data = jsonObj.map(x => {
      let y = {};
      y['Full Name P Name Full Name'] = x.FullName;
      y['Last Name'] = x.LastName;
      y['First Name'] = x.FirstName;
      y['Title P Job Title'] = x.Title;
      y['Company Name C Name'] = addQuotes(x.CompanyName);
      y['Phone P Phone Work'] = x.Phone;
      y['Address C Address: Main Address 1'] = x.Address;
      y['Suite C Address: Other Adress 2'] = x.Suite;
      y['City C City'] = x.City;
      y['State C State'] = x.State;
      y['Zip C Zip'] = x.Zip;
      y['Tenant: Web Address C URL'] = x.TenantWebAddress;
      y['Tenant: SIC Description C SIC Description'] = addQuotes(x.TenantSICDescription);
      y['Tenant: SF Occupied C Custom SF Occupied'] = x.TenantSFOccupied;
      y['Tenant: Location C  Custom Location Type'] = x.TenantLocation;
      y['Tenant: # Emp C Custom Employee Count'] = x.TenantEmp;
      y['Ten_Phone C Phone work'] = x.TenPhone;
      y['Move Date C Custom Move Date'] = x.MoveDate;
      y['Mailing Address: Address P Address: Main Address 1'] = x.MailingAddressAddress;
      y['Mailing Address: City P Address: Main City'] = x.MailingAddressCity;
      y['Mailing Address: State P Address: Main State'] = x.MailingAddressState;
      y['Mailing Address: Zip P Address: Main State'] = x.MailingAddressZip;
      y['C Tag'] = `"Industrial", "Tenant"`;
      y['C Description'] = 'Tenant';

      return y;
    });
    fs.writeFileSync('./data/tenant_result.json', JSON.stringify(data, null, 2));
    const parser = new Parser({
      quote: '',
    });
    const csv = parser.parse(data);
    // console.log(csv);
    fs.writeFileSync('./data/tenant_result.csv', csv);
  })
}

function processFiles(){
  processOwnerFile();
  processTenantFile();
}

processFiles();
