"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.debugAuth = debugAuth;
const axios_1 = __importDefault(require("axios"));
function debugAuth(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        var _a, _b, _c, _d, _e, _f;
        try {
            const WP_BASE_URL = process.env.WP_BASE_URL;
            const WP_USERNAME = process.env.WP_USERNAME;
            const WP_APP_PASSWORD = process.env.WP_APP_PASSWORD;
            console.log('🔐 Debug Authentication Configuration');
            console.log('URL:', WP_BASE_URL);
            console.log('Username:', WP_USERNAME);
            console.log('Password Length:', WP_APP_PASSWORD === null || WP_APP_PASSWORD === void 0 ? void 0 : WP_APP_PASSWORD.length);
            console.log('Password (first/last 4):', (WP_APP_PASSWORD === null || WP_APP_PASSWORD === void 0 ? void 0 : WP_APP_PASSWORD.substring(0, 4)) + '...' + (WP_APP_PASSWORD === null || WP_APP_PASSWORD === void 0 ? void 0 : WP_APP_PASSWORD.substring(WP_APP_PASSWORD.length - 4)));
            // Test 1: API WordPress de base
            console.log('🧪 Test 1: Basic WordPress API');
            const credentials = Buffer.from(`${WP_USERNAME}:${WP_APP_PASSWORD}`).toString('base64');
            const test1 = yield axios_1.default.get(`${WP_BASE_URL}/wp-json/`, {
                headers: {
                    'Authorization': `Basic ${credentials}`,
                    'User-Agent': 'CCIS-Connect-API/1.0'
                },
                timeout: 10000
            });
            // Test 2: Gravity Forms API
            console.log('🧪 Test 2: Gravity Forms API');
            const test2 = yield axios_1.default.get(`${WP_BASE_URL}/wp-json/gf/v2/forms`, {
                headers: {
                    'Authorization': `Basic ${credentials}`,
                    'User-Agent': 'CCIS-Connect-API/1.0'
                },
                timeout: 10000
            });
            return reply.code(200).send({
                success: true,
                tests: {
                    basic_api: {
                        status: test1.status,
                        data: Object.keys(test1.data)
                    },
                    gravity_forms: {
                        status: test2.status,
                        forms_count: Object.keys(test2.data).length,
                        forms: Object.keys(test2.data)
                    }
                }
            });
        }
        catch (error) {
            console.error('❌ Debug Auth Failed:');
            console.error('Error Message:', error.message);
            console.error('Status:', (_a = error.response) === null || _a === void 0 ? void 0 : _a.status);
            console.error('Status Text:', (_b = error.response) === null || _b === void 0 ? void 0 : _b.statusText);
            console.error('Response Data:', (_c = error.response) === null || _c === void 0 ? void 0 : _c.data);
            return reply.code(500).send({
                success: false,
                error: {
                    message: error.message,
                    status: (_d = error.response) === null || _d === void 0 ? void 0 : _d.status,
                    status_text: (_e = error.response) === null || _e === void 0 ? void 0 : _e.statusText,
                    data: (_f = error.response) === null || _f === void 0 ? void 0 : _f.data
                },
                troubleshooting: [
                    "Vérifiez que le mot de passe d'application est correct",
                    "Vérifiez que l'utilisateur a les permissions administrateur",
                    "Vérifiez que l'API REST WordPress est activée",
                    "Vérifiez que Gravity Forms est installé et activé"
                ]
            });
        }
    });
}
