"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.workingTest = workingTest;
exports.getBusinessPlanWorking = getBusinessPlanWorking;
const gravity_forms_working_1 = require("../../services/gravity-forms-working");
function workingTest(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        var _a;
        try {
            console.log('🎯 WORKING TEST - Version corrigée');
            const connectionTest = yield gravity_forms_working_1.gravityFormsWorkingService.testConnection();
            if (!connectionTest.success) {
                return reply.code(401).send({
                    success: false,
                    message: "Échec de la connexion",
                    error: connectionTest.message,
                    debug: {
                        base_url: process.env.WP_BASE_URL,
                        username: process.env.WP_USERNAME,
                        password_length: (_a = process.env.WP_APP_PASSWORD) === null || _a === void 0 ? void 0 : _a.length
                    }
                });
            }
            const forms = yield gravity_forms_working_1.gravityFormsWorkingService.getAllForms();
            return reply.code(200).send({
                success: true,
                message: "✅ Connexion réussie avec la version corrigée!",
                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('❌ Working test failed:', error.message);
            return reply.code(500).send({
                success: false,
                message: "Erreur lors du test fonctionnel",
                error: error.message,
                debug: {
                    base_url: process.env.WP_BASE_URL,
                    username: process.env.WP_USERNAME
                }
            });
        }
    });
}
function getBusinessPlanWorking(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            console.log('🔄 Fetching Business Plan data (Working version)...');
            const entries = yield gravity_forms_working_1.gravityFormsWorkingService.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 Working 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
            });
        }
    });
}
