const db = require('../../lib/db');
const { listen } = require('../../lib/db/notify');
const { ALERT, ERROR, WARNING, NOTICE, INFO, DEBUG } = require('DOUGAL_ROOT/debug')(__filename);

const timeout = 30*1000;

async function init () {
	INFO("Setting up task");

	// Full list of channels in
	// ../lib/db/channels
	const channels = [
		"project",
		"preplot_lines", "preplot_points",
		"raw_lines", "raw_shots",
		"final_lines", "final_shots"
	];

	const throttlePeriod = 10*1000;

	const projects = {};

	listen (channels, (data) => {
		// Something important has changed,
		// set the dirty flag for the relevant project
		const pid = data.payload?.pid ?? data.payload?.new?.pid ?? data.payload?.old?.pid;
		if (pid) {
			if (!pid in projects) {
				projects[pid] = {
					lastRefreshed: 0
				};
			}
			if (!projects[pid].needsRefresh) {
				projects[pid].needsRefresh = true;
				DEBUG("Setting up refresh flag for %s: %j", pid, projects[pid]);
			}
		}
	});

	const task = async () => {
		for (pid in projects) {
			const project = projects[pid];
			if (project.needsRefresh) {
				const now = Date.now();
				const lastRefreshAge = now - project.lastRefreshed;
				if (lastRefreshAge > throttlePeriod) {
					// Do the actual refresh
					try {
						DEBUG("Refreshing", pid);
						await db.project.summary.refresh(pid);
					} catch (err) {
						if (err.status == 404) {
							DEBUG("Project %s not found. Removing from refresh list", pid);
							delete projects[pid];
						} else {
							ERROR(err);
						}
					}
					project.needsRefresh = false;
					project.lastRefreshed = now;
				}
			}
		}
	};

	// Let us populate the project list and do a first refresh on startup
	for (const project of await db.project.get()) {
		projects[project.pid] = {
			lastRefreshed: 0,
			needsRefresh: true
		}
	}
	task(); // No need to await

	return task;
}

async function cleanup () {
}

module.exports = {
	init,
	timeout,
	cleanup
};
