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

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

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

	return {
		movies,
		actors,
		genres,
	};
}

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

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

	await utilsApi.sql(`create table movies_liked (
		 id int,
		 user_id int,
		 movie_id int
	)`);

	const { movies, actors, genres } = await fetchMovies();

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

	const docs = movies
		.map((movie) => ({
			replace: {
				index: 'movies',
				id: movie.id,
				doc: {
					title: movie.title,
					actor_ids: movie.cast.map((actor) => actors.indexOf(actor)),
					actors: movie.cast.join(','),
					genre_ids: movie.genres.map((genre) => genres.indexOf(genre)),
					genres: movie.genres.join(','),
				},
			},
		}))
		.concat(likedMovieIds.map((movieId, index) => ({
			replace: {
				index: 'movies_liked',
				id: index + 1,
				doc: {
					user_id: Math.floor(Math.random() * 51),
					movie_id: movieId,
				},
			},
		})));

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

	console.log('data', data);

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

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

init();
