const fs = require('fs-extra');
const jsonToCsv = require('./json-to-csv');

function writeClaims(id, data) {
  const path = `mock-api/resources/${id}/claims`;
  if (!fs.existsSync(path)) {
    fs.mkdirSync(path);
  }

  const promises = [];
  promises.push(fs.writeFile(`${path}/contents.csv`, `${jsonToCsv(data.content)}\n`));

  const claim = { ...data };
  claim.field_definition = {};
  claim.manifest = {};
  claim.content = {};

  promises.push(fs.writeFile(`${path}/claim.json`, `${JSON.stringify(claim, null, 2)}\n`));
  promises.push(
    fs.writeFile(`${path}/manifest.json`, `${JSON.stringify(data.manifest, null, 2)}\n`),
  );
  promises.push(
    fs.writeFile(
      `${path}/field-definitions.json`,
      `${JSON.stringify(data.field_definition, null, 2)}\n`,
    ),
  );

  return Promise.all(promises);
}

module.exports = writeClaims;
