import { Types } from 'mongoose'
import { ScheduleEvent, ScheduleEventHandler } from '../../utils/types'
import { User } from '../users/models'
import { IUser } from '../users/models'
import services from '../../utils/services'

const smartMatchSchedules = {} as Record<ScheduleEvent, ScheduleEventHandler>

smartMatchSchedules['smart-match-feedback'] = async (data: Record<string, any>) => {
	interface Payload {
		smartMatch: string
		chat: string
		userA: string
		userB: string
		community: string
		schedule: string
	}
	const { smartMatch, chat, userA, userB } = data as Payload

	const users = await User.find({ _id: { $in: [userA, userB] } })
	for (const u of users) {
		const otherUser = users.find(other => `${other._id}` !== `${u._id}`)
		await services.notifyUser({
			user: u as unknown as IUser,
			title: 'How was your Vibe Match? ✦',
			description: `How was your match with ${otherUser?.username}? Your feedback helps us improve!`,
			event: 'smart-match-feedback',
			smartMatch: new Types.ObjectId(smartMatch),
			chat: new Types.ObjectId(chat),
		})
	}
}

export default smartMatchSchedules
