const axios = require('axios');

/**
 * Template Manager Client
 * Handles communication with the template-manager service
 */
class TemplateManagerClient {
  constructor() {
    this.baseURL = process.env.TEMPLATE_MANAGER_URL || 'http://localhost:8009';
    this.timeout = 30000; // 30 seconds
  }

  /**
   * Get permutation recommendations for a template
   */
  async getPermutationRecommendations(templateId, options = {}) {
    try {
      const url = `${this.baseURL}/api/enhanced-ckg-tech-stack/permutations/${templateId}`;
      
      const response = await axios.get(url, {
        timeout: this.timeout,
        headers: {
          'Content-Type': 'application/json'
        }
      });

      return {
        success: true,
        data: response.data,
        source: 'template-manager',
        type: 'permutations'
      };

    } catch (error) {
      console.error('❌ Template Manager permutation error:', error.message);
      return {
        success: false,
        error: error.message,
        source: 'template-manager',
        type: 'permutations'
      };
    }
  }

  /**
   * Get combination recommendations for a template
   */
  async getCombinationRecommendations(templateId, options = {}) {
    try {
      const url = `${this.baseURL}/api/enhanced-ckg-tech-stack/combinations/${templateId}`;
      
      const response = await axios.get(url, {
        timeout: this.timeout,
        headers: {
          'Content-Type': 'application/json'
        }
      });

      return {
        success: true,
        data: response.data,
        source: 'template-manager',
        type: 'combinations'
      };

    } catch (error) {
      console.error('❌ Template Manager combination error:', error.message);
      return {
        success: false,
        error: error.message,
        source: 'template-manager',
        type: 'combinations'
      };
    }
  }

  /**
   * Get comprehensive recommendations (both permutations and combinations)
   */
  async getComprehensiveRecommendations(templateId, options = {}) {
    try {
      const url = `${this.baseURL}/api/enhanced-ckg-tech-stack/recommendations/${templateId}`;
      
      const response = await axios.get(url, {
        timeout: this.timeout,
        headers: {
          'Content-Type': 'application/json'
        }
      });

      return {
        success: true,
        data: response.data,
        source: 'template-manager',
        type: 'comprehensive'
      };

    } catch (error) {
      console.error('❌ Template Manager comprehensive error:', error.message);
      return {
        success: false,
        error: error.message,
        source: 'template-manager',
        type: 'comprehensive'
      };
    }
  }

  /**
   * Get template features
   */
  async getTemplateFeatures(templateId) {
    try {
      const url = `${this.baseURL}/api/templates/${templateId}/features`;
      
      const response = await axios.get(url, {
        timeout: this.timeout,
        headers: {
          'Content-Type': 'application/json'
        }
      });

      return {
        success: true,
        data: response.data,
        source: 'template-manager',
        type: 'template-features'
      };

    } catch (error) {
      console.error('❌ Template Manager features error:', error.message);
      return {
        success: false,
        error: error.message,
        source: 'template-manager',
        type: 'template-features'
      };
    }
  }

  /**
   * Get template information
   */
  async getTemplateInfo(templateId) {
    try {
      const url = `${this.baseURL}/api/templates/${templateId}`;
      
      const response = await axios.get(url, {
        timeout: this.timeout,
        headers: {
          'Content-Type': 'application/json'
        }
      });

      return {
        success: true,
        data: response.data,
        source: 'template-manager',
        type: 'template-info'
      };

    } catch (error) {
      console.error('❌ Template Manager template info error:', error.message);
      return {
        success: false,
        error: error.message,
        source: 'template-manager',
        type: 'template-info'
      };
    }
  }

  /**
   * Check service health
   */
  async checkHealth() {
    try {
      const url = `${this.baseURL}/health`;
      
      const response = await axios.get(url, {
        timeout: 5000,
        headers: {
          'Content-Type': 'application/json'
        }
      });

      return {
        success: true,
        status: 'healthy',
        responseTime: response.headers['x-response-time'] || 'unknown'
      };

    } catch (error) {
      console.error('❌ Template Manager health check error:', error.message);
      return {
        success: false,
        status: 'unhealthy',
        error: error.message
      };
    }
  }
}

module.exports = TemplateManagerClient;
