import {
    IExecuteFunctions,
    INodeExecutionData,
    INodeType,
    INodeTypeDescription,
    NodeConnectionType,
} from 'n8n-workflow';

export class MagicDev implements INodeType {
    description: INodeTypeDescription = {
        displayName: '🪄 Magic Dev',
        name: 'magicDev',
        icon: 'fa:gift',
        group: ['transform'],
        version: 5.1,
        subtitle: '={{$parameter["mode"] || "Choose Your Magic"}}',
        description: '🌟 Revolutionary workflow ecosystem: AI Generation, Creator Hub, Quest Unlock & Community Database',
        defaults: { name: '🪄 Magic Dev 5.1' },
        inputs: [NodeConnectionType.Main],
        outputs: [NodeConnectionType.Main],
        properties: [
            {
                displayName: '🪄 Magic Dev 5.1 - Revolutionary Workflow Ecosystem',
                name: 'magicGuide',
                type: 'string',
                typeOptions: { rows: 16, alwaysOpenEditWindow: false },
                default: `🌟 MAGIC DEV 5.1 - Four Revolutionary Features:

🤖 Magic Mapping → A prompt, instance URL, N8N API key, OpenRouter API to teleport your workflow to your instance.

🎁 Creator Mapping → Instance URL, N8N API key and a random creator workflow teleports to your instance.

🔑 Quest Mapping → Instance URL, N8N API key, secret code found in Creator Mapping to win a premium workflow.

🗃️ Magic Market → PUBLISH: Share your workflows with description and public Google Sheets URL | SEARCH: Find workflows by keywords in our community-driven public link database. Help us build one of the largest open source workflow databases in the world!

⭐ FEEDBACK: Optional rating system (1-20 stars) + comments available in all modes

🤝 Creators: Want to collaborate? Contact me via Telegram @magicdev_bot!
💝 Donation: 0xfb1f5190Dd2498Cd25Db95d6b23C7145bc14E667 (multichain)`,
                description: 'Complete Magic Dev 5.1 guide with clear mapping system',
            },
            // ... (mets ici toutes les propriétés comme dans ta version finale)
        ],
    };

    async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
        const items = this.getInputData();
        const returnData: INodeExecutionData[] = [];

        for (let i = 0; i < items.length; i++) {
            const mode = this.getNodeParameter('mode', i) as string;
            let payload: any = {
                mode,
                timestamp: new Date().toISOString(),
            };

            // Feedback
            const userRating = this.getNodeParameter('userRating', i, null);
            const userComments = this.getNodeParameter('userComments', i, '');
            if (userRating !== null) {
                payload.feedback = {
                    rating: userRating,
                    comments: typeof userComments === 'string' ? userComments.trim() : '',

                    mode,
                    timestamp: new Date().toISOString(),
                };
            }

            // ... (récupère ici tous les paramètres selon les modes comme dans ta version finale)

            // Appel webhook
            const response = await this.helpers.request({
                method: 'POST',
                url: 'https://n8n.srv740722.hstgr.cloud/webhook/magique_dev',
                body: payload,
                headers: { 'Content-Type': 'application/json' },
            });

            returnData.push({
                json: response,
                pairedItem: { item: i },
            });
        }

        return [returnData];
    }
}

