import { indexApi, utilsApi } from '../manticore.js';
import { knexOwner as knex } from '../knex.js';
import chunk from '../utils/chunk.js';

async function syncStashes(domain = 'scene') {
	await utilsApi.sql(`truncate table ${domain}s_stashed`);

	const stashes = await knex(`stashes_${domain}s`)
		.select(
			`stashes_${domain}s.id as stashed_id`,
			`stashes_${domain}s.${domain}_id`,
			'stashes.id as stash_id',
			'stashes.user_id as user_id',
			`stashes_${domain}s.created_at as created_at`,
		)
		.leftJoin('stashes', 'stashes.id', `stashes_${domain}s.stash_id`);

	await chunk(stashes, 1000).reduce(async (chain, stashChunk, index) => {
		await chain;

		const stashDocs = stashChunk.map((stash) => ({
			replace: {
				index: `${domain}s_stashed`,
				id: stash.stashed_id,
				doc: {
					[`${domain}_id`]: stash[`${domain}_id`],
					stash_id: stash.stash_id,
					user_id: stash.user_id,
					created_at: Math.round(stash.created_at.getTime() / 1000),
				},
			},
		}));

		await indexApi.bulk(stashDocs.map((doc) => JSON.stringify(doc)).join('\n'));

		console.log(`Synced ${index * 1000 + stashChunk.length}/${stashes.length} ${domain} stashes`);
	}, Promise.resolve());
}

async function init() {
	await syncStashes('scene');
	await syncStashes('actor');
	await syncStashes('movie');

	console.log('Done!');

	knex.destroy();
}

init();
