import { fetchActors } from '../actors.js';

export function curateActorsQuery(query) {
	return {
		query: query.q,
		gender: query.gender,
		age: query.age?.split(',').map((age) => Number(age)),
		dateOfBirth: query.dob && new Date(query.dob),
		dobType: ({ dob: 'dateOfBirth', bd: 'birthday' })[query.dobt] || 'birthday',
		cup: query.cup?.split(','),
		country: query.c,
		naturalBoobs: query.nb,
		height: query.height?.split(',').map((height) => Number(height)),
		weight: query.weight?.split(',').map((weight) => Number(weight)),
		requireAvatar: query.avatar,
	};
}

export async function fetchActorsApi(req, res) {
	const {
		actors,
		countries,
		limit,
		total,
	} = await fetchActors(curateActorsQuery(req.query), {
		page: Number(req.query.page) || 1,
		limit: Number(req.query.limit) || 120,
		order: req.query.order?.split('.') || ['likes', 'desc'],
	});

	res.send({
		actors,
		countries,
		limit,
		total,
	});
}
