const db = require('../../db');
const render = require('../../render');
const seisjson = require('./seisjson');
const filename = require('./filename');

// FIXME Refactor when able
const defaultTemplatePath = require('path').resolve(__dirname, "../../../../../../etc/default/templates/sequence.html.njk");


async function html ({project, sequence}, seisdata) {
	const sequenceData = async () => db.sequence.get(project, sequence, {summary: true});

	const template = (await db.configuration.get(project, "sse/templates/0/template")) || defaultTemplatePath;
	const json = seisdata?.data ?? await seisjson({project, sequence});
	const data = await render(json, template);
	const name = filename(seisdata?.name || await sequenceData(), ".html");

	return { name, data, mimetype: "text/html" };
}

module.exports = html;
