import { GuildScheduledEvent, TextChannel } from "discord.js";
import { v4 as uuid } from "uuid";
import { client } from "../..";
import { config } from "../configuration";
import { createDateStringFromEvent } from "../helper/dateHelper";
import { Maybe } from "../interfaces";
import { logger } from "../logger";


export const name = 'guildScheduledEventCreate'

export async function execute(event: GuildScheduledEvent) {
	const guildId = event.guildId
	const requestId = uuid()
	try {
		if (!event.description) {
			logger.debug("Got GuildScheduledEventCreate event. But has no description. Aborting.")
			return
		}

		if (event.description.includes("!wp")) {
			logger.info("Got manual create event of watchparty event!", { guildId, requestId })
			if (event.description.includes("!private")) {
				logger.info("Event description contains \"!private\". Won't announce.", { guildId, requestId })
				return
			}

			const channel: Maybe<TextChannel> = client.getAnnouncementChannelForGuild(guildId)

			if (!channel) {
				logger.error("Could not obtain announcement channel. Aborting announcement.", { guildId, requestId })
				return
			}

			if (!event.scheduledStartAt) {
				logger.error('Event has no start date, bailing out')
				return
			}
			const message = `[Watchparty] https://discord.com/events/${event.guildId}/${event.id} \nHey <@&${config.bot.announcement_role}>, wir gucken ${event.name} ${createDateStringFromEvent(event.scheduledStartAt, guildId, requestId)}`

			channel.send(message)
		} else {
			logger.debug("Got GuildScheduledEventCreate event but no !wp in description. Not creating manual wp announcement.", { guildId, requestId })
		}

	} catch (error) {
		// sendFailureDM(error)
		logger.error(<string>error, { guildId, requestId })
	}


}
