"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.finalTest = finalTest;
exports.getBusinessPlanFinal = getBusinessPlanFinal;
const gravity_forms_final_1 = require("../../services/gravity-forms-final");
function finalTest(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            console.log('🎯 FINAL TEST - Nouveau mot de passe');
            const connectionTest = yield gravity_forms_final_1.gravityFormsFinalService.testConnection();
            if (!connectionTest.success) {
                return reply.code(401).send({
                    success: false,
                    message: "Échec de la connexion",
                    error: connectionTest.message,
                    troubleshooting: [
                        "Vérifiez que le mot de passe d'application est correctement copié",
                        "Vérifiez que l'utilisateur ccismsm a les permissions administrateur",
                        "Vérifiez que Gravity Forms est installé et activé"
                    ]
                });
            }
            const forms = yield gravity_forms_final_1.gravityFormsFinalService.getAllForms();
            return reply.code(200).send({
                success: true,
                message: "✅ Connexion réussie avec le nouveau mot de passe!",
                connection: connectionTest,
                forms: {
                    total: Object.keys(forms).length,
                    list: Object.keys(forms).map(id => ({
                        id,
                        title: forms[id].title,
                        entries: forms[id].entries || 0
                    }))
                }
            });
        }
        catch (error) {
            console.error('❌ Final test failed:', error.message);
            return reply.code(500).send({
                success: false,
                message: "Erreur lors du test final",
                error: error.message
            });
        }
    });
}
function getBusinessPlanFinal(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            console.log('🔄 Fetching Business Plan data (Final)...');
            const entries = yield gravity_forms_final_1.gravityFormsFinalService.getFormEntries('7');
            return reply.code(200).send({
                success: true,
                message: "Données Business Plan récupérées avec succès",
                metadata: {
                    total_entries: entries.length,
                    generated_at: new Date().toISOString()
                },
                entries: entries
            });
        }
        catch (error) {
            console.error('❌ Business Plan Final Error:', 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
            });
        }
    });
}
