import axios from 'axios';

/**
 * Centralized API service for all component settings
 */
export const apiService = {
  /**
   * Generic API call for saving settings
   * @param {string} endpoint - API endpoint
   * @param {object} data - Data to send
   * @returns {Promise} - API response
   */
  async saveSettings(endpoint, data) {
    const url = `${appLocalizer.wpntsUrl}/wpnts/v1/${endpoint}`;

    try {
      const response = await axios.post(url, data, {
        headers: {
          'content-type': 'application/json',
          'X-WP-NONCE': appLocalizer.nonce
        }
      });
      return response;
    } catch (error) {
      console.error('API Error:', error);
      throw error;
    }
  },

  // Specific endpoints for each component
  async saveAuthorSettings(data) {
    return this.saveSettings('slack_webhook_interval', { wpntswebhook: data });
  },

  async saveCommentSettings(data) {
    return this.saveSettings('slack_webhook_comment_settings', { wpntswebhook_comment_settings: data });
  },

  async saveCaptchaSettings(data) {
    return this.saveSettings('slack_webhook_wpnts_captcha_settings', { wpnts_captcha_settings: data });
  },

  async saveWooCommerceSettings(data) {
    return this.saveSettings('slack_webhook_interval_woocommerce_settings', { wpntswebhook_woocommerce_settings: data });
  },

  async saveSecuritySettings(data) {
    return this.saveSettings('slack_webhook_site_security_settings', { wpntswebhook_site_settings: data });
  },

  async saveMediaSettings(data) {
    return this.saveSettings('slack_webhook_media_settings', { wpntswebhook_media_settings: data });
  },

  async saveDebuggerSettings(data) {
    return this.saveSettings('slack_webhook_wpntswebhook_debug_log', { wpntswebhook_debug_log: data });
  },

  async cleanDebugLog() {
    return this.saveSettings('slack_webhook_wpntswebhook_debug_log_clean', { cleanDebugLog: 1 });
  },

  async saveDowntimeAlertsSettings(data) {
    return this.saveSettings('slack_website_downtime_alerts', { wpntswebhook_website_downtime_alerts: data });
  },

  async saveEmergencySettings(data) {
    return this.saveSettings('slack_webhook_wpntswebhook_emengency_shutdown', { wpntswebhook_emengency_shutdown: data });
  },

  async getEmergencyToken() {
    const url = `${appLocalizer.wpntsUrl}/wpnts/v1/get_emergency_token`;
    const response = await fetch(url, {
      headers: {
        'content-type': 'application/json',
        'X-WP-NONCE': appLocalizer.nonce
      }
    });
    return response.json();
  },

  async getEmergencyActivationLogs() {
    const url = `${appLocalizer.wpntsUrl}/wpnts/v1/emergency_activation_logs`;
    const response = await fetch(url, {
      headers: {
        'content-type': 'application/json',
        'X-WP-NONCE': appLocalizer.nonce
      }
    });
    return response.json();
  },

  async clearEmergencyLogs() {
    const url = `${appLocalizer.wpntsUrl}/wpnts/v1/clear_emergency_logs`;
    const response = await fetch(url, {
      method: 'POST',
      headers: {
        'content-type': 'application/json',
        'X-WP-NONCE': appLocalizer.nonce
      },
      body: JSON.stringify({})
    });
    return response.json();
  },

  async saveMaintenanceSettings(data) {
    return this.saveSettings('slack_webhook_wpntsmaintenannotice_log', { wpntsmaintenannotice_log: data });
  },

  async saveMenuSettings(data) {
    return this.saveSettings('slack_webhook_menu_settings', { wpntswebhook_menu_settings: data });
  },

  async savePageNPostSettings(data) {
    return this.saveSettings('slack_webhook_pagenpost_settings', { wpntswebhook_pagenpost_settings: data });
  },

  async saveSiteSettings(data) {
    return this.saveSettings('slack_webhook_interval_site_settings', { wpntswebhook_site_settings: data });
  },

  async saveThemeSettings(data) {
    return this.saveSettings('slack_webhook_theme_settings', { wpntswebhook_theme_settings: data });
  },

  async saveWpwidgetsSettings(data) {
    return this.saveSettings('slack_webhook_widget_settings', { wpntswebhook_widget_settings: data });
  },

  async saveAnalyticsSettings(data) {
    return this.saveSettings('slack_webhook_analytics_settings', { wpntswebhook_analytics_settings: data });
  },

  async verifyCaptcha(sitekeys, secretkey, token) {
    return this.saveSettings('slack_webhook_wpnts_captcha_verify', {
      sitekeys,
      secretkey,
      token
    });
  }
};