import { ButtonInteraction, ModalSubmitInteraction, StringSelectMenuInteraction } from "discord.js";
import { TRADE_CUSTOM_IDS } from "@modules/trade/trade.types";
import { SHOP_CUSTOM_IDS, LOOTDROP_CUSTOM_IDS } from "@modules/economy/economy.types";
import { ITEM_WIZARD_CUSTOM_IDS } from "@modules/admin/item_wizard.types";
import { TRIVIA_CUSTOM_IDS } from "@modules/trivia/trivia.types";
import { ENROLLMENT_CUSTOM_IDS } from "@modules/user/user.types";
import { FEEDBACK_CUSTOM_IDS } from "@modules/feedback/feedback.types";

// Union type for all component interactions
type ComponentInteraction = ButtonInteraction | StringSelectMenuInteraction | ModalSubmitInteraction;

// Type for the dynamically imported module containing the handler
interface InteractionModule {
    [key: string]: (...args: any[]) => Promise<void> | any;
}

// Route definition
interface InteractionRoute {
    predicate: (interaction: ComponentInteraction) => boolean;
    handler: () => Promise<InteractionModule>;
    method: string;
}

export const interactionRoutes: InteractionRoute[] = [
    // --- TRADE MODULE ---
    {
        predicate: (i) => i.customId.startsWith(TRADE_CUSTOM_IDS.PREFIX) || i.customId === TRADE_CUSTOM_IDS.MONEY_AMOUNT_FIELD,
        handler: () => import("@/modules/trade/trade.interaction"),
        method: 'handleTradeInteraction'
    },

    // --- ECONOMY MODULE ---
    {
        predicate: (i) => i.isButton() && i.customId.startsWith(SHOP_CUSTOM_IDS.BUY_PREFIX),
        handler: () => import("@/modules/economy/shop.interaction"),
        method: 'handleShopInteraction'
    },
    {
        predicate: (i) => i.isButton() && i.customId.startsWith(LOOTDROP_CUSTOM_IDS.PREFIX),
        handler: () => import("@/modules/economy/lootdrop.interaction"),
        method: 'handleLootdropInteraction'
    },
    {
        predicate: (i) => i.isButton() && i.customId.startsWith(TRIVIA_CUSTOM_IDS.PREFIX),
        handler: () => import("@/modules/trivia/trivia.interaction"),
        method: 'handleTriviaInteraction'
    },

    // --- ADMIN MODULE ---
    {
        predicate: (i) => i.customId.startsWith(ITEM_WIZARD_CUSTOM_IDS.PREFIX),
        handler: () => import("@/modules/admin/item_wizard"),
        method: 'handleItemWizardInteraction'
    },

    // --- USER MODULE ---
    {
        predicate: (i) => i.isButton() && i.customId === ENROLLMENT_CUSTOM_IDS.ENROLL,
        handler: () => import("@/modules/user/enrollment.interaction"),
        method: 'handleEnrollmentInteraction'
    },

    // --- FEEDBACK MODULE ---
    {
        predicate: (i) => i.customId.startsWith(FEEDBACK_CUSTOM_IDS.PREFIX),
        handler: () => import("@/modules/feedback/feedback.interaction"),
        method: 'handleFeedbackInteraction'
    }
];
