const axios = require('axios');

/**
 * Tech Stack Selector Client
 * Handles communication with the tech-stack-selector service
 */
class TechStackSelectorClient {
  constructor() {
    this.baseURL = process.env.TECH_STACK_SELECTOR_URL || 'http://localhost:8002';
    this.timeout = 30000; // 30 seconds
  }

  /**
   * Get tech stack recommendations based on budget and domain
   */
  async getTechStackRecommendations(budget, domain, features = []) {
    try {
      const url = `${this.baseURL}/recommend/stack`;
      
      // Convert budget string to numeric value
      let numericBudget = parseFloat(budget);
      if (isNaN(numericBudget)) {
        // Map budget strings to numeric values
        const budgetMap = {
          'micro': 15,
          'startup': 75,
          'small': 200,
          'medium': 450,
          'large': 800,
          'enterprise': 1500
        };
        numericBudget = budgetMap[budget.toLowerCase()] || 450; // Default to medium
      }
      
      const payload = {
        budget: numericBudget,
        domain: domain,
        features: features
      };

      console.log('🔍 Tech Stack Selector payload:', JSON.stringify(payload, null, 2));

      const response = await axios.post(url, payload, {
        timeout: this.timeout,
        headers: {
          'Content-Type': 'application/json'
        }
      });

      return {
        success: true,
        data: response.data,
        source: 'tech-stack-selector',
        type: 'domain-based'
      };

    } catch (error) {
      console.error('❌ Tech Stack Selector recommendation error:', error.message);
      return {
        success: false,
        error: error.message,
        source: 'tech-stack-selector',
        type: 'domain-based'
      };
    }
  }

  /**
   * Get recommendations by budget only
   */
  async getRecommendationsByBudget(budget) {
    try {
      const url = `${this.baseURL}/recommend/budget`;
      
      const payload = {
        budget: parseFloat(budget)
      };

      const response = await axios.post(url, payload, {
        timeout: this.timeout,
        headers: {
          'Content-Type': 'application/json'
        }
      });

      return {
        success: true,
        data: response.data,
        source: 'tech-stack-selector',
        type: 'budget-based'
      };

    } catch (error) {
      console.error('❌ Tech Stack Selector budget error:', error.message);
      return {
        success: false,
        error: error.message,
        source: 'tech-stack-selector',
        type: 'budget-based'
      };
    }
  }

  /**
   * Get recommendations by domain only
   */
  async getRecommendationsByDomain(domain) {
    try {
      const url = `${this.baseURL}/recommend/domain`;
      
      const payload = {
        domain: domain
      };

      const response = await axios.post(url, payload, {
        timeout: this.timeout,
        headers: {
          'Content-Type': 'application/json'
        }
      });

      return {
        success: true,
        data: response.data,
        source: 'tech-stack-selector',
        type: 'domain-only'
      };

    } catch (error) {
      console.error('❌ Tech Stack Selector domain error:', error.message);
      return {
        success: false,
        error: error.message,
        source: 'tech-stack-selector',
        type: 'domain-only'
      };
    }
  }

  /**
   * Get AI-powered recommendations
   */
  async getAIRecommendations(requirements) {
    try {
      const url = `${this.baseURL}/recommend/ai`;
      
      const response = await axios.post(url, requirements, {
        timeout: this.timeout,
        headers: {
          'Content-Type': 'application/json'
        }
      });

      return {
        success: true,
        data: response.data,
        source: 'tech-stack-selector',
        type: 'ai-powered'
      };

    } catch (error) {
      console.error('❌ Tech Stack Selector AI error:', error.message);
      return {
        success: false,
        error: error.message,
        source: 'tech-stack-selector',
        type: 'ai-powered'
      };
    }
  }

  /**
   * Get available domains
   */
  async getAvailableDomains() {
    try {
      const url = `${this.baseURL}/domains`;
      
      const response = await axios.get(url, {
        timeout: this.timeout,
        headers: {
          'Content-Type': 'application/json'
        }
      });

      return {
        success: true,
        data: response.data,
        source: 'tech-stack-selector',
        type: 'domains'
      };

    } catch (error) {
      console.error('❌ Tech Stack Selector domains error:', error.message);
      return {
        success: false,
        error: error.message,
        source: 'tech-stack-selector',
        type: 'domains'
      };
    }
  }

  /**
   * 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('❌ Tech Stack Selector health check error:', error.message);
      return {
        success: false,
        status: 'unhealthy',
        error: error.message
      };
    }
  }
}

module.exports = TechStackSelectorClient;
