import type { EventSubChannelChatMessageEvent } from "@twurple/eventsub-base";
import { api } from "index";
import { chatterId, streamerId } from "main";
import type User from "user";

export type userType = "chatter" | "admin" | "streamer" | "moderator";

export type specialExecuteArgs = {
	activation?: string;
};

export interface commandOptions {
	name: string;
	aliases: string[];
	usertype: userType;
	execution: (
		message: EventSubChannelChatMessageEvent,
		sender: User,
		args?: specialExecuteArgs,
	) => Promise<void>;
	disableable?: boolean;
	specialaliases?: string[];
}

/** The Command class represents a command */
export class Command {
	public readonly name: string;
	public readonly aliases: string[];
	public readonly usertype: userType;
	public readonly disableable: boolean;
	public readonly specialaliases: string[];
	public readonly execute: (
		message: EventSubChannelChatMessageEvent,
		sender: User,
		args?: specialExecuteArgs,
	) => Promise<void>;
	constructor(options: commandOptions) {
		this.name = options.name.toLowerCase();
		this.aliases = options.aliases;
		this.usertype = options.usertype;
		this.execute = options.execution;
		this.disableable = options.disableable ?? true;
		this.specialaliases = options.specialaliases ?? [];
	}
}

/** Helper function to send a message to the stream */
export const sendMessage = async (
	message: string,
	replyParentMessageId?: string,
) => {
	try {
		return await api.chat.sendChatMessageAsApp(chatterId, streamerId, message, {
			replyParentMessageId,
		});
	} catch (_e) {
		return await api.chat.sendChatMessageAsApp(chatterId, streamerId, message);
	}
};
