import Discord, { MessageActionRow, MessageButton } from "discord.js";
import EmojiEmbed from "../utils/generateEmojiEmbed.js";
import getEmojiByName from "../utils/getEmojiByName.js";
import client from "../utils/client.js";

export async function create(
    guild: Discord.Guild,
    member: Discord.User,
    createdBy: Discord.User,
    reason: string | null,
    customReason?: string
) {
    const config = await client.database.guilds.read(guild.id);
    const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger;
    const overwrites = [
        {
            id: member,
            allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "ATTACH_FILES", "ADD_REACTIONS", "READ_MESSAGE_HISTORY"],
            type: "member"
        }
    ] as Discord.OverwriteResolvable[];
    overwrites.push({
        id: guild.roles.everyone,
        deny: ["VIEW_CHANNEL"],
        type: "role"
    });
    if (config.tickets.supportRole !== null) {
        overwrites.push({
            id: guild.roles.cache.get(config.tickets.supportRole)!,
            allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "ATTACH_FILES", "ADD_REACTIONS", "READ_MESSAGE_HISTORY"],
            type: "role"
        });
    }

    let c;
    try {
        c = await guild.channels.create(member.username, {
            type: "GUILD_TEXT",
            topic: `${member.id} Active`,
            parent: config.tickets.category,
            nsfw: false,
            permissionOverwrites: overwrites as Discord.OverwriteResolvable[],
            reason: "Creating ticket"
        });
    } catch (e) {
        return null;
    }
    try {
        await c.send({
            content:
                `<@${member.id}>` + (config.tickets.supportRole !== null ? ` • <@&${config.tickets.supportRole}>` : ""),
            allowedMentions: {
                users: [member.id],
                roles: config.tickets.supportRole !== null ? [config.tickets.supportRole] : []
            }
        });
        await c.send({
            embeds: [
                new EmojiEmbed()
                    .setTitle("New Ticket")
                    .setDescription(
                        "Ticket created by a Moderator\n" +
                            `**Support type:** ${customReason ? customReason : "Appeal submission"}\n` +
                            (reason !== null ? `**Reason:**\n> ${reason}\n` : "") +
                            `**Ticket ID:** \`${c.id}\`\n` +
                            "Type `/ticket close` to close this ticket."
                    )
                    .setStatus("Success")
                    .setEmoji("GUILD.TICKET.OPEN")
            ],
            components: [
                new MessageActionRow().addComponents([
                    new MessageButton()
                        .setLabel("Close")
                        .setStyle("DANGER")
                        .setCustomId("closeticket")
                        .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
                ])
            ]
        });
        const data = {
            meta: {
                type: "ticketCreate",
                displayName: "Ticket Created",
                calculateType: "ticketUpdate",
                color: NucleusColors.green,
                emoji: "GUILD.TICKET.OPEN",
                timestamp: new Date().getTime()
            },
            list: {
                ticketFor: entry(member.id, renderUser(member)),
                createdBy: entry(createdBy.id, renderUser(createdBy)),
                created: entry(new Date().getTime(), renderDelta(new Date().getTime())),
                ticketChannel: entry(c.id, renderChannel(c))
            },
            hidden: {
                guild: guild.id
            }
        };
        log(data);
    } catch (e) {
        console.log(e);
        return null;
    }
    return c.id;
}

export async function areTicketsEnabled(guild: string) {
    const config = await client.database.guilds.read(guild);
    return config.tickets.enabled;
}
