import { EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, Colors } from "discord.js";
import { PRUNE_CUSTOM_IDS, type PruneResult, type PruneProgress } from "./prune.types";

/**
 * Creates a confirmation message for prune operations
 */
export function getConfirmationMessage(
    amount: number | 'all',
    estimatedCount?: number
): { embeds: EmbedBuilder[], components: ActionRowBuilder<ButtonBuilder>[] } {
    const isAll = amount === 'all';
    const messageCount = isAll ? estimatedCount : amount;

    const embed = new EmbedBuilder()
        .setTitle("⚠️ Confirm Deletion")
        .setDescription(
            isAll
                ? `You are about to delete **ALL messages** in this channel.\n\n` +
                `Estimated messages: **~${estimatedCount || 'Unknown'}**\n` +
                `This action **cannot be undone**.`
                : `You are about to delete **${amount} messages**.\n\n` +
                `This action **cannot be undone**.`
        )
        .setColor(Colors.Orange)
        .setTimestamp();

    const confirmButton = new ButtonBuilder()
        .setCustomId(PRUNE_CUSTOM_IDS.CONFIRM)
        .setLabel("Confirm")
        .setStyle(ButtonStyle.Danger);

    const cancelButton = new ButtonBuilder()
        .setCustomId(PRUNE_CUSTOM_IDS.CANCEL)
        .setLabel("Cancel")
        .setStyle(ButtonStyle.Secondary);

    const row = new ActionRowBuilder<ButtonBuilder>()
        .addComponents(confirmButton, cancelButton);

    return { embeds: [embed], components: [row] };
}

/**
 * Creates a progress embed for ongoing deletions
 */
export function getProgressEmbed(progress: PruneProgress): EmbedBuilder {
    const percentage = Math.round((progress.current / progress.total) * 100);

    return new EmbedBuilder()
        .setTitle("🔄 Deleting Messages")
        .setDescription(
            `Progress: **${progress.current}/${progress.total}** (${percentage}%)\n\n` +
            `Please wait...`
        )
        .setColor(Colors.Blue)
        .setTimestamp();
}

/**
 * Creates a success embed after deletion
 */
export function getSuccessEmbed(result: PruneResult): EmbedBuilder {
    let description = `Successfully deleted **${result.deletedCount} messages**.`;

    if (result.filtered && result.username) {
        description = `Successfully deleted **${result.deletedCount} messages** from **${result.username}**.`;
    }

    if (result.skippedOld && result.skippedOld > 0) {
        description += `\n\n⚠️ **${result.skippedOld} messages** were older than 14 days and could not be deleted.`;
    }

    if (result.deletedCount < result.requestedCount && !result.skippedOld) {
        description += `\n\nℹ️ Only **${result.deletedCount}** messages were available to delete.`;
    }

    return new EmbedBuilder()
        .setTitle("✅ Messages Deleted")
        .setDescription(description)
        .setColor(Colors.Green)
        .setTimestamp();
}

/**
 * Creates an error embed
 */
export function getPruneErrorEmbed(message: string): EmbedBuilder {
    return new EmbedBuilder()
        .setTitle("❌ Prune Failed")
        .setDescription(message)
        .setColor(Colors.Red)
        .setTimestamp();
}

/**
 * Creates a warning embed
 */
export function getPruneWarningEmbed(message: string): EmbedBuilder {
    return new EmbedBuilder()
        .setTitle("⚠️ Warning")
        .setDescription(message)
        .setColor(Colors.Yellow)
        .setTimestamp();
}

/**
 * Creates a cancelled embed
 */
export function getCancelledEmbed(): EmbedBuilder {
    return new EmbedBuilder()
        .setTitle("🚫 Deletion Cancelled")
        .setDescription("Message deletion has been cancelled.")
        .setColor(Colors.Grey)
        .setTimestamp();
}
