import config from 'config';

import knex from './knex.js';
import { searchApi } from './manticore.js';
import { HttpError } from './errors.js';
import { fetchActorsById, curateActor, sortActorsByGender } from './actors.js';
import { fetchTagsById } from './tags.js';
import { fetchEntitiesById } from './entities.js';

function curateMedia(media) {
	if (!media) {
		return null;
	}

	return {
		id: media.id,
		path: media.path,
		thumbnail: media.thumbnail,
		lazy: media.lazy,
		isS3: media.is_s3,
		width: media.width,
		height: media.height,
	};
}

function curateMovie(rawScene, assets) {
	if (!rawScene) {
		return null;
	}

	return {
		id: rawScene.id,
		title: rawScene.title,
		slug: rawScene.slug,
		url: rawScene.url,
		date: rawScene.date,
		createdAt: rawScene.created_at,
		effectiveDate: rawScene.effective_date,
		description: rawScene.description,
		duration: rawScene.duration,
		channel: {
			id: assets.channel.id,
			slug: assets.channel.slug,
			name: assets.channel.name,
			type: assets.channel.type,
			isIndependent: assets.channel.independent,
			hasLogo: assets.channel.has_logo,
		},
		network: assets.channel.network_id ? {
			id: assets.channel.network_id,
			slug: assets.channel.network_slug,
			name: assets.channel.network_name,
			type: assets.channel.network_type,
			hasLogo: assets.channel.has_logo,
		} : null,
		actors: sortActorsByGender(assets.actors.map((actor) => curateActor(actor, { sceneDate: rawScene.effective_date }))),
		directors: assets.directors.map((director) => ({
			id: director.id,
			slug: director.slug,
			name: director.name,
		})),
		tags: assets.tags.map((tag) => ({
			id: tag.id,
			slug: tag.slug,
			name: tag.name,
		})),
		poster: curateMedia(assets.poster),
		covers: assets.covers.map((cover) => curateMedia(cover)),
		photos: assets.photos.map((photo) => curateMedia(photo)),
		createdBatchId: rawScene.created_batch_id,
		updatedBatchId: rawScene.updated_batch_id,
	};
}

export async function fetchMoviesById(movieIds) {
	const [movies, channels, actors, directors, tags, covers, photos] = await Promise.all([
		knex('movies').whereIn('id', movieIds),
		// channels
		knex('movies')
			.select('channels.*', 'networks.id as network_id', 'networks.slug as network_slug', 'networks.name as network_name', 'networks.type as network_type')
			.whereIn('movies.id', movieIds)
			.leftJoin('entities as channels', 'channels.id', 'movies.entity_id')
			.leftJoin('entities as networks', 'networks.id', 'channels.parent_id')
			.groupBy('channels.id', 'networks.id'),
		// actors
		knex('movies')
			.select(
				'actors.*',
				'actors_meta.*',
				'releases_actors.release_id',
				'movies.id as movie_id',
			)
			.distinctOn('actors.id', 'movies.id') // cannot distinct on JSON column avatar, must specify
			.whereIn('movies.id', movieIds)
			.whereNotNull('actors.id')
			.leftJoin('movies_scenes', 'movies_scenes.movie_id', 'movies.id')
			.leftJoin('releases_actors', 'releases_actors.release_id', 'movies_scenes.scene_id')
			.leftJoin('actors', 'actors.id', 'releases_actors.actor_id')
			.leftJoin('actors_meta', 'actors_meta.actor_id', 'actors.id'),
		// directors
		knex('movies')
			.whereIn('movies.id', movieIds)
			.leftJoin('movies_scenes', 'movies_scenes.movie_id', 'movies.id')
			.leftJoin('releases_directors', 'releases_directors.release_id', 'movies_scenes.scene_id')
			.leftJoin('actors as directors', 'directors.id', 'releases_directors.director_id'),
		// tags
		knex('movies')
			.select('tags.id', 'tags.slug', 'tags.name', 'tags.priority', 'movies.id as movie_id')
			.distinct()
			.whereIn('movies.id', movieIds)
			.whereNotNull('tags.id')
			.leftJoin('movies_scenes', 'movies_scenes.movie_id', 'movies.id')
			.leftJoin('releases_tags', 'releases_tags.release_id', 'movies_scenes.scene_id')
			.leftJoin('tags', 'tags.id', 'releases_tags.tag_id')
			.orderBy('priority', 'desc'),
		// covers
		knex('movies_covers')
			.whereIn('movie_id', movieIds)
			.leftJoin('media', 'media.id', 'movies_covers.media_id'),
		// photos
		knex('movies')
			.whereIn('movies.id', movieIds)
			.leftJoin('movies_scenes', 'movies_scenes.movie_id', 'movies.id')
			.leftJoin('releases_photos', 'releases_photos.release_id', 'movies_scenes.scene_id')
			.leftJoin('media', 'media.id', 'releases_photos.media_id'),
	]);

	return movieIds.map((movieId) => {
		const movie = movies.find((movieEntry) => movieEntry.id === movieId);

		if (!movie) {
			console.warn('cannot find movie', movieId);
			return null;
		}

		const movieChannel = channels.find((entity) => entity.id === movie.entity_id);
		const movieActors = actors.filter((actor) => actor.movie_id === movieId);
		const movieDirectors = directors.filter((director) => director.release_id === movieId);
		const movieTags = tags.filter((tag) => tag.movie_id === movieId);
		const movieCovers = covers.filter((cover) => cover.movie_id === movieId);
		const moviePhotos = photos.filter((photo) => photo.release_id === movieId);

		return curateMovie(movie, {
			channel: movieChannel,
			actors: movieActors,
			directors: movieDirectors,
			tags: movieTags,
			covers: movieCovers,
			photos: moviePhotos,
		});
	}).filter(Boolean);
}

function curateOptions(options) {
	if (options?.limit > 100) {
		throw new HttpError('Limit must be <= 100', 400);
	}

	return {
		limit: options?.limit || 30,
		page: Number(options?.page) || 1,
		aggregate: options.aggregate ?? true,
		aggregateActors: (options.aggregate ?? true) && (options.aggregateActors ?? true),
		aggregateTags: (options.aggregate ?? true) && (options.aggregateTags ?? true),
		aggregateChannels: (options.aggregate ?? true) && (options.aggregateChannels ?? true),
	};
}

function buildQuery(filters = {}) {
	const query = {
		bool: {
			must: [],
		},
	};

	let sort = [{ effective_date: 'desc' }];

	if (!filters.scope || filters.scope === 'latest') {
		query.bool.must.push({
			range: {
				effective_date: {
					lte: Math.round(Date.now() / 1000),
				},
			},
		});
	}

	if (filters.scope === 'upcoming') {
		query.bool.must.push({
			range: {
				effective_date: {
					gt: Math.round(Date.now() / 1000),
				},
			},
		});

		sort = [{ effective_date: 'asc' }];
	}

	if (filters.scope === 'new') {
		sort = [{ created_at: 'desc' }, { effective_date: 'asc' }];
	}

	if (filters.scope === 'likes') {
		sort = [{ stashed: 'desc' }, { effective_date: 'desc' }];
	}

	if (filters.scope === 'results') {
		sort = [{ _score: 'desc' }, { effective_date: 'desc' }];
	}

	if (filters.query) {
		query.bool.must.push({
			bool: {
				should: [
					{ match: { title_filtered: filters.query } },
					{ match: { actors: filters.query } },
					{ match: { tags: filters.query } },
					{ match: { channel_name: filters.query } },
					{ match: { network_name: filters.query } },
					{ match: { channel_slug: filters.query } },
					{ match: { network_slug: filters.query } },
					{ match: { meta: filters.query } }, // date
				],
			},
		});
	}

	if (filters.tagIds) {
		filters.tagIds.forEach((tagId) => {
			query.bool.must.push({ equals: { 'any(tag_ids)': tagId } });
		});
	}

	if (filters.entityId) {
		query.bool.must.push({
			bool: {
				should: [
					{ equals: { channel_id: filters.entityId } },
					{ equals: { network_id: filters.entityId } },
				],
			},
		});
	}

	if (filters.actorIds) {
		filters.actorIds.forEach((actorId) => {
			query.bool.must.push({ equals: { 'any(actor_ids)': actorId } });
		});
	}

	if (filters.requireCover) {
		query.bool.must.push({
			equals: {
				has_cover: 1,
			},
		});
	}

	/* tag filter
	must_not: [
		{
			in: {
				'any(tag_ids)': [101, 180, 32],
			},
		},
	],
	*/

	return { query, sort };
}

function buildAggregates(options) {
	const aggregates = {};

	if (options.aggregateActors) {
		aggregates.actorIds = {
			terms: {
				field: 'actor_ids',
				size: config.database.manticore.maxAggregateSize,
			},
			// sort: [{ 'count(*)': { order: 'desc' } }],
		};
	}

	if (options.aggregateTags) {
		aggregates.tagIds = {
			terms: {
				field: 'tag_ids',
				size: config.database.manticore.maxAggregateSize,
			},
		};
	}

	if (options.aggregateChannels) {
		aggregates.channelIds = {
			terms: {
				field: 'channel_id',
				size: config.database.manticore.maxAggregateSize,
			},
		};
	}

	return aggregates;
}

function countAggregations(buckets) {
	if (!buckets) {
		return null;
	}

	return Object.fromEntries(buckets.map((bucket) => [bucket.key, { count: bucket.doc_count }]));
}

export async function fetchMovies(filters, rawOptions) {
	const options = curateOptions(rawOptions);
	const { query, sort } = buildQuery(filters);

	console.log('filters', filters);
	console.log('options', options);
	console.log('query', query.bool.must);

	console.time('manticore');

	const result = await searchApi.search({
		index: 'movies',
		query,
		limit: options.limit,
		offset: (options.page - 1) * options.limit,
		sort,
		aggs: buildAggregates(options),
		options: {
			max_matches: config.database.manticore.maxMatches,
			max_query_time: config.database.manticore.maxQueryTime,
			field_weights: {
				title_filtered: 7,
				actors: 10,
				tags: 9,
				meta: 6,
				channel_name: 2,
				channel_slug: 3,
				network_name: 1,
				network_slug: 1,
			},
		},
	});

	console.timeEnd('manticore');

	const actorCounts = options.aggregateActors && countAggregations(result.aggregations?.actorIds?.buckets);
	const tagCounts = options.aggregateTags && countAggregations(result.aggregations?.tagIds?.buckets);
	const channelCounts = options.aggregateChannels && countAggregations(result.aggregations?.channelIds?.buckets);

	console.time('fetch aggregations');

	const [aggActors, aggTags, aggChannels] = await Promise.all([
		options.aggregateActors ? fetchActorsById(result.aggregations.actorIds.buckets.map((bucket) => bucket.key), { order: ['name', 'asc'], append: actorCounts }) : [],
		options.aggregateTags ? fetchTagsById(result.aggregations.tagIds.buckets.map((bucket) => bucket.key), { order: ['name', 'asc'], append: tagCounts }) : [],
		options.aggregateChannels ? fetchEntitiesById(result.aggregations.channelIds.buckets.map((bucket) => bucket.key), { order: ['name', 'asc'], append: channelCounts }) : [],
	]);

	console.timeEnd('fetch aggregations');

	const movieIds = result.hits.hits.map((hit) => Number(hit._id));
	const movies = await fetchMoviesById(movieIds);

	return {
		movies,
		aggActors,
		aggTags,
		aggChannels,
		total: result.hits.total,
		limit: options.limit,
	};
}
