import { indexApi, utilsApi } from '../manticore.js';
import rawvideos from './movies.json' with { type: 'json' };

async function fetchvideos() {
	const videos = rawvideos
		.filter((video) => video.cast.length > 0
			&& video.genres.length > 0
			&& video.cast.every((actor) => actor.charCodeAt(0) >= 65)) // throw out videos with non-alphanumerical actor names
		.map((video, index) => ({ id: index + 1, ...video }));

	const actors = Array.from(new Set(videos.flatMap((video) => video.cast))).sort();
	const genres = Array.from(new Set(videos.flatMap((video) => video.genres)));

	return {
		videos,
		actors,
		genres,
	};
}

async function init() {
	await utilsApi.sql('drop table if exists videos');
	await utilsApi.sql('drop table if exists videos_liked');

	await utilsApi.sql(`create table videos (
		 id int,
		 title text,
		 actor_ids multi,
		 actors text,
		 genre_ids multi,
		 genres text
	)`);

	await utilsApi.sql(`create table videos_liked (
		 id int,
		 user_id int,
		 video_id int
	)`);

	const { videos, actors, genres } = await fetchvideos();

	const likedvideoIds = Array.from(new Set(Array.from({ length: 10.000 }, () => videos[Math.round(Math.random() * videos.length)].id)));

	const docs = videos
		.map((video) => ({
			replace: {
				index: 'videos',
				id: video.id,
				doc: {
					title: video.title,
					actor_ids: video.cast.map((actor) => actors.indexOf(actor)),
					actors: video.cast.join(','),
					genre_ids: video.genres.map((genre) => genres.indexOf(genre)),
					genres: video.genres.join(','),
				},
			},
		}))
		.concat(likedvideoIds.map((videoId, index) => ({
			replace: {
				index: 'videos_liked',
				id: index + 1,
				doc: {
					user_id: Math.floor(Math.random() * 51),
					video_id: videoId,
				},
			},
		})));

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

	console.log('data', data);

	const result = await utilsApi.sql(`
		select * from videos_liked
		limit 10
	`);

	console.log(result[0].data);
	console.log(result[1]);
}

init();
