import axios from "axios";
import { mcpConfig } from "../config/index.js";
import { log } from "../utils/index.js";
class WordPressAPIClient {
    client;
    apiUrl;
    apiKey;
    constructor() {
        this.apiUrl = mcpConfig.wordpress.apiUrl;
        this.apiKey = mcpConfig.wordpress.apiKey;
        this.client = axios.create({
            baseURL: this.apiUrl,
            headers: {
                "Content-Type": "application/json",
                "X-DF-Key": this.apiKey,
                "User-Agent": "MCP-WordPress-API-Client/1.0",
            },
            timeout: 30000,
        });
        this.client.interceptors.request.use((config) => {
            log("info", `API Request: ${config.method?.toUpperCase()} ${config.url}`);
            return config;
        }, (error) => {
            log("error", "API Request Error:", error);
            return Promise.reject(error);
        });
        this.client.interceptors.response.use((response) => {
            log("info", `API Response: ${response.status} ${response.statusText}`);
            return response;
        }, (error) => {
            if (error.response) {
                log("error", `API Error Response: ${error.response.status}`, error.response.data);
            }
            else if (error.request) {
                log("error", "API No Response:", error.request);
            }
            else {
                log("error", "API Request Setup Error:", error.message);
            }
            return Promise.reject(error);
        });
    }
    async executeQuery(sql) {
        try {
            const response = await this.client.post("/query", {
                query: sql,
            });
            if (response.data.success) {
                return {
                    success: true,
                    data: response.data.data,
                    count: response.data.count,
                    query: response.data.query,
                };
            }
            else {
                throw new Error(response.data.message || "Query execution failed");
            }
        }
        catch (error) {
            if (error.response?.data) {
                const apiError = error.response.data;
                throw new Error(apiError.message || `API Error: ${error.response.status}`);
            }
            throw error;
        }
    }
    async getSchema(tableName) {
        try {
            const url = tableName ? `/schema?table=${tableName}` : "/schema";
            const response = await this.client.get(url);
            if (response.data.success) {
                return response.data;
            }
            else {
                throw new Error(response.data.message || "Schema retrieval failed");
            }
        }
        catch (error) {
            if (error.response?.data) {
                const apiError = error.response.data;
                throw new Error(apiError.message || `API Error: ${error.response.status}`);
            }
            throw error;
        }
    }
    async testConnection() {
        try {
            await this.getSchema();
            return true;
        }
        catch (error) {
            log("error", "Connection test failed:", error);
            return false;
        }
    }
}
export const wpApiClient = new WordPressAPIClient();
