"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBusinessPlanCorrected = getBusinessPlanCorrected;
exports.testConnection = testConnection;
const gravity_forms_service_corrected_1 = require("../../services/gravity-forms-service-corrected");
function getBusinessPlanCorrected(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            console.log('🔄 Fetching Business Plan data (corrected)...');
            // Test de connexion d'abord
            yield gravity_forms_service_corrected_1.gravityFormsServiceCorrected.testConnection();
            const entries = yield gravity_forms_service_corrected_1.gravityFormsServiceCorrected.getFormEntries('7');
            const legitEntries = entries.filter(entry => entry.is_legit);
            console.log(`✅ Business Plan: ${legitEntries.length}/${entries.length} legitimate entries`);
            return reply.code(200).send({
                success: true,
                metadata: {
                    total: entries.length,
                    legitimate: legitEntries.length,
                    spam: entries.length - legitEntries.length,
                    percentage_legit: entries.length > 0 ? Math.round((legitEntries.length / entries.length) * 100) : 0
                },
                entries: legitEntries
            });
        }
        catch (error) {
            console.error('❌ Business Plan Error (corrected):', error.message);
            return reply.code(500).send({
                success: false,
                message: "Erreur lors de la récupération des données Business Plan",
                error: error.message,
                troubleshooting: [
                    "Vérifiez le mot de passe d'application WordPress",
                    "Vérifiez que l'utilisateur a les permissions Gravity Forms",
                    "Vérifiez que le formulaire Business Plan (ID 7) existe"
                ]
            });
        }
    });
}
function testConnection(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            console.log('🧪 Testing corrected service connection...');
            const result = yield gravity_forms_service_corrected_1.gravityFormsServiceCorrected.testConnection();
            const forms = yield gravity_forms_service_corrected_1.gravityFormsServiceCorrected.getAllForms();
            return reply.code(200).send({
                success: true,
                message: "Connexion WordPress API réussie",
                connection: result,
                forms: {
                    count: Object.keys(forms).length,
                    available: Object.keys(forms).map(id => ({
                        id,
                        title: forms[id].title
                    }))
                },
                timestamp: new Date().toISOString()
            });
        }
        catch (error) {
            console.error('❌ Connection test failed:', error.message);
            return reply.code(500).send({
                success: false,
                message: "Échec de la connexion WordPress API",
                error: error.message,
                timestamp: new Date().toISOString()
            });
        }
    });
}
