import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import fs from 'fs-extra';
import { selectFiles } from './filechooser.js';
import path from 'path';

const argv = yargs(hideBin(process.argv)).argv


(async () => {
    const result = await selectFiles({startingPath:'./data',directoryFilter: (directoryName)=>{
        return false;
      },fileFilter: (fileName) => {
        return /\.json$/gi.test(fileName);
      },pageSize:10,multi:false});
    if (argv.delete){ // delete==business or commercialProperty
      const ids = await fetch(`http://localhost:3000/bizmatch/listings/undefined/repo/${argv.delete}`,{
        method: 'GET',
        headers: { 'Content-Type': 'application/json' },
      })
      await fetch(`http://localhost:3000/bizmatch/listings/undefined/repo/${argv.delete}`,{
        method: 'DELETE',
        headers: { 'Content-Type': 'application/json' },
      })        
    }
    console.log(result['selectedFiles'][0]);
    const file = result['selectedFiles'][0];
    const listings = await fs.readJson(file);
    for (const listing of listings) {
        const type = listing.listingsCategory
        const response = await fetch(`http://localhost:3000/bizmatch/listings/${type}`, {
            method: 'POST',
            body: JSON.stringify(listing),
            headers: { 'Content-Type': 'application/json' },
        });
    }
})();
