import {
    ModalBuilder,
    TextInputBuilder,
    TextInputStyle,
    ActionRowBuilder,
    StringSelectMenuBuilder,
    ActionRowBuilder as MessageActionRowBuilder,
    ContainerBuilder,
    TextDisplayBuilder,
    ButtonBuilder,
    ButtonStyle
} from "discord.js";
import { FEEDBACK_TYPE_LABELS, FEEDBACK_CUSTOM_IDS, type FeedbackData, type FeedbackType } from "./feedback.types";

export function getFeedbackTypeMenu() {
    const select = new StringSelectMenuBuilder()
        .setCustomId(FEEDBACK_CUSTOM_IDS.SELECT_TYPE)
        .setPlaceholder("Choose feedback type")
        .addOptions([
            {
                label: "💡 Feature Request",
                description: "Suggest a new feature or improvement",
                value: "FEATURE_REQUEST"
            },
            {
                label: "🐛 Bug Report",
                description: "Report a bug or issue",
                value: "BUG_REPORT"
            },
            {
                label: "💬 General Feedback",
                description: "Share your thoughts or suggestions",
                value: "GENERAL"
            }
        ]);

    const row = new MessageActionRowBuilder<StringSelectMenuBuilder>().addComponents(select);
    return { components: [row] };
}

export function getFeedbackModal(feedbackType: FeedbackType) {
    const modal = new ModalBuilder()
        .setCustomId(`${FEEDBACK_CUSTOM_IDS.MODAL}_${feedbackType}`)
        .setTitle(FEEDBACK_TYPE_LABELS[feedbackType]);

    // Title Input
    const titleInput = new TextInputBuilder()
        .setCustomId(FEEDBACK_CUSTOM_IDS.TITLE_FIELD)
        .setLabel("Title")
        .setStyle(TextInputStyle.Short)
        .setPlaceholder("Brief summary of your feedback")
        .setRequired(true)
        .setMaxLength(100);

    const titleRow = new ActionRowBuilder<TextInputBuilder>().addComponents(titleInput);

    // Description Input
    const descriptionInput = new TextInputBuilder()
        .setCustomId(FEEDBACK_CUSTOM_IDS.DESCRIPTION_FIELD)
        .setLabel("Description")
        .setStyle(TextInputStyle.Paragraph)
        .setPlaceholder("Provide detailed information about your feedback")
        .setRequired(true)
        .setMaxLength(1000);

    const descriptionRow = new ActionRowBuilder<TextInputBuilder>().addComponents(descriptionInput);

    modal.addComponents(titleRow, descriptionRow);

    return modal;
}

export function buildFeedbackMessage(feedback: FeedbackData) {
    // Define colors/themes for each feedback type
    const themes = {
        FEATURE_REQUEST: {
            icon: "💡",
            color: "Blue",
            title: "FEATURE REQUEST",
            description: "A new starlight suggestion has been received"
        },
        BUG_REPORT: {
            icon: "🐛",
            color: "Red",
            title: "BUG REPORT",
            description: "A cosmic anomaly has been detected"
        },
        GENERAL: {
            icon: "💬",
            color: "Gray",
            title: "GENERAL FEEDBACK",
            description: "A message from the cosmos"
        }
    };

    const theme = themes[feedback.type];

    if (!theme) {
        console.error(`Unknown feedback type: ${feedback.type}`);
        throw new Error(`Invalid feedback type: ${feedback.type}`);
    }

    const timestamp = Math.floor(feedback.timestamp.getTime() / 1000);

    // Header Container
    const headerContainer = new ContainerBuilder()
        .addTextDisplayComponents(
            new TextDisplayBuilder().setContent(`# ${theme.icon} ${theme.title}`),
            new TextDisplayBuilder().setContent(`*${theme.description}*`)
        );

    // Content Container
    const contentContainer = new ContainerBuilder()
        .addTextDisplayComponents(
            new TextDisplayBuilder().setContent(`## ${feedback.title}`),
            new TextDisplayBuilder().setContent(`> ${feedback.description.split('\n').join('\n> ')}`),
            new TextDisplayBuilder().setContent(
                `**Submitted by:** <@${feedback.userId}>\n**Time:** <t:${timestamp}:F> (<t:${timestamp}:R>)`
            )
        );

    return [headerContainer, contentContainer];
}
