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,
    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;
}
