
//import fetch from 'node-fetch'; // For ES Modules
const fs = require('fs');
const path = require('path');
const { parse } = require('json2csv');
const striptags = require('striptags');
const dayjs = require('dayjs');
const he = require('he');
const jsonTojsonl = require('json-to-jsonl');

const fetch = require('node-fetch'); // For CommonJS
const { start } = require('repl');
const request = require('request');
let { render } = require("mustache");

let jsonlData = '';

const DATA_INPUT_FOLDER = './input';
const DATA_OUTPUT_FOLDER = './output';
const TEMPLATES_FOLDER = './templates';
const DIRECTUS_BUCKET = 'https://ewr1.vultrobjects.com/lgcc-conferences/'
const LOCALE = 'es'

function isUndefined(variable){
    if( typeof(variable) === 'undefined'){
        return true
    } else {
        return false
    }
}

async function generateFiles( type ) {
  let options = { json: true };

  let url = `http://conferences.local:10003/v3/conferencias/?f=id_list&cache=true&locale=${LOCALE}`;

  request(url, options, (error, res, body) => {
    if (error) {
      return console.log(error)
    };

    if (!error && res.statusCode == 200) {
      // do something with JSON, using the 'body' variable
      const items = body.result.conferencias;

      let nitems = items.map((item) => {
        let nitem = {}

        nitem.locale = LOCALE
        nitem.code = `${LOCALE}-${dayjs(item.date).format('YYYYMMDD')}-${item.activity}`
        nitem.id = item.wpid
        nitem.title = item.title
        nitem.date = item.date;
        nitem.activity = item.activity;
        
        nitem.thumbnail_file = item.thumbnail ? `${item.fileslug}.${item.thumbnail?.toString().split('.')[1]}` : ''
        nitem.thumbnail_id = item.thumbnail

        nitem.video_file = item.video ? `${item.fileslug}.${item.video?.toString().split('.')[1]}` : ''
        nitem.video_id = item.video ? item.video : ''
        
        nitem.audio_file = item.audio ? `${item.fileslug}.mp3` : ''
        nitem.audio_id = item.audio
        
        nitem.booklet_file = item.pdf ? `${item.fileslug}_booklet.pdf` : ''
        nitem.booklet_id = item.pdf
        
        nitem.simple_file = item.pdf_simple ? `${item.fileslug}.pdf` : ''
        nitem.simple_id = item.pdf_simple
      
        return nitem;
      })
      
      if( nitems.length > 0 ){
        let csv = parse(nitems);
        jsonlData += csv
        writeFile(jsonlData, type)
        }
    };
  });

}

function writeFile(jsonlData, type) {
  fs.writeFile(`./${DATA_INPUT_FOLDER}/conferences_${LOCALE}_files.csv`, jsonlData, (err) => {
    if (err) {
      console.error("Error writing file:", err);
      return;
    }
    console.log("File has been written successfully! : " + type);
  });
}

generateFiles( 'conferences' );