import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ModalBuilder, StringSelectMenuBuilder, TextInputBuilder, TextInputStyle } from "discord.js";
import { createBaseEmbed } from "@lib/embeds";
import type { TradeSession, TradeParticipant } from "./trade.types";

const EMBED_COLOR = 0xFFD700; // Gold

function formatOffer(participant: TradeParticipant) {
    let text = "";
    if (participant.offer.money > 0n) {
        text += `💰 ${participant.offer.money} 🪙\n`;
    }
    if (participant.offer.items.length > 0) {
        text += participant.offer.items.map((i) => `- ${i.name} (x${i.quantity})`).join("\n");
    }
    if (text === "") text = "*Empty Offer*";
    return text;
}

export function getTradeDashboard(session: TradeSession) {
    const embed = createBaseEmbed("🤝 Trading Session", undefined, EMBED_COLOR)
        .addFields(
            {
                name: `${session.userA.username} ${session.userA.locked ? '✅ (Ready)' : '✏️ (Editing)'}`,
                value: formatOffer(session.userA),
                inline: true
            },
            {
                name: `${session.userB.username} ${session.userB.locked ? '✅ (Ready)' : '✏️ (Editing)'}`,
                value: formatOffer(session.userB),
                inline: true
            }
        )
        .setFooter({ text: "Both parties must click Lock to confirm trade." });

    const row = new ActionRowBuilder<ButtonBuilder>()
        .addComponents(
            new ButtonBuilder().setCustomId('trade_add_item').setLabel('Add Item').setStyle(ButtonStyle.Secondary),
            new ButtonBuilder().setCustomId('trade_add_money').setLabel('Add Money').setStyle(ButtonStyle.Success),
            new ButtonBuilder().setCustomId('trade_remove_item').setLabel('Remove Item').setStyle(ButtonStyle.Secondary),
            new ButtonBuilder().setCustomId('trade_lock').setLabel('Lock / Unlock').setStyle(ButtonStyle.Primary),
            new ButtonBuilder().setCustomId('trade_cancel').setLabel('Cancel').setStyle(ButtonStyle.Danger),
        );

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

export function getTradeCompletedEmbed(session: TradeSession) {
    const embed = createBaseEmbed("✅ Trade Completed", undefined, "Green")
        .addFields(
            { name: session.userA.username, value: formatOffer(session.userA), inline: true },
            { name: session.userB.username, value: formatOffer(session.userB), inline: true }
        )
        .setTimestamp();

    return embed;
}

export function getTradeMoneyModal() {
    const modal = new ModalBuilder()
        .setCustomId('trade_money_modal')
        .setTitle('Add Money');

    const input = new TextInputBuilder()
        .setCustomId('amount')
        .setLabel("Amount to trade")
        .setStyle(TextInputStyle.Short)
        .setPlaceholder("100")
        .setRequired(true);

    const row = new ActionRowBuilder<TextInputBuilder>().addComponents(input);
    modal.addComponents(row);

    return modal;
}

export function getItemSelectMenu(items: { label: string, value: string, description?: string }[], customId: string, placeholder: string) {
    const select = new StringSelectMenuBuilder()
        .setCustomId(customId)
        .setPlaceholder(placeholder)
        .addOptions(items);

    const row = new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(select);

    return { components: [row] };
}
