/**
 * Mock Tech Stack Analyzer Service
 * Generates mock tech stack recommendations for testing when Claude API is unavailable
 */
class MockTechStackAnalyzer {
  constructor() {
    this.model = 'mock-analyzer-v1.0';
    this.timeout = 1000; // Fast mock responses
  }

  /**
   * Generate mock tech stack recommendations
   * @param {Object} templateData - Complete template data with features and business rules
   * @returns {Promise<Object>} - Mock tech stack recommendations
   */
  async analyzeTemplate(templateData) {
    const startTime = Date.now();
    
    try {
      console.log(`🤖 [MockAnalyzer] Generating mock recommendations for template: ${templateData.title}`);
      
      // Simulate processing time
      await new Promise(resolve => setTimeout(resolve, 500));
      
      const processingTime = Date.now() - startTime;
      
      // Generate mock recommendations based on template category
      const recommendations = this.generateMockRecommendations(templateData);
      
      console.log(`✅ [MockAnalyzer] Mock analysis completed in ${processingTime}ms for template: ${templateData.title}`);
      
      return {
        ...recommendations,
        analysis_context: {
          template_title: templateData.title,
          template_category: templateData.category,
          features_count: templateData.features?.length || 0,
          business_rules_count: templateData.business_rules?.length || 0
        },
        processing_time_ms: processingTime,
        ai_model: this.model,
        analysis_version: '1.0',
        status: 'completed'
      };
      
    } catch (error) {
      console.error(`❌ [MockAnalyzer] Mock analysis failed for template ${templateData.title}:`, error.message);
      throw error;
    }
  }

  /**
   * Generate mock recommendations based on template data
   * @param {Object} templateData - Template data
   * @returns {Object} - Mock recommendations
   */
  generateMockRecommendations(templateData) {
    const category = templateData.category?.toLowerCase() || 'general';
    
    // Base recommendations - multiple technology options per category
    const baseRecommendations = {
      frontend: [
        {
          technology: 'React',
          confidence: 0.85,
          reasoning: 'React is the top choice for modern web applications with component-based architecture',
          rank: 1
        },
        {
          technology: 'Next.js',
          confidence: 0.80,
          reasoning: 'Next.js is an excellent alternative as it builds on React with built-in SSR and routing capabilities',
          rank: 2
        },
        {
          technology: 'Vue.js',
          confidence: 0.75,
          reasoning: 'Vue.js offers a simpler learning curve and excellent performance for modern applications',
          rank: 3
        }
      ],
      backend: [
        {
          technology: 'Node.js',
          confidence: 0.80,
          reasoning: 'Node.js is the optimal backend choice for JavaScript-based applications with excellent scalability',
          rank: 1
        },
        {
          technology: 'Python',
          confidence: 0.75,
          reasoning: 'Python offers excellent libraries and frameworks for various application domains',
          rank: 2
        },
        {
          technology: 'Java',
          confidence: 0.70,
          reasoning: 'Java provides enterprise-grade stability and scalability for long-term applications',
          rank: 3
        }
      ],
      mobile: [
        {
          technology: 'React Native',
          confidence: 0.75,
          reasoning: 'React Native is the best cross-platform mobile solution leveraging React knowledge',
          rank: 1
        },
        {
          technology: 'Flutter',
          confidence: 0.70,
          reasoning: 'Flutter offers excellent performance and a single codebase for both iOS and Android platforms',
          rank: 2
        },
        {
          technology: 'Ionic',
          confidence: 0.65,
          reasoning: 'Ionic provides web-based mobile development with native capabilities',
          rank: 3
        }
      ],
      testing: [
        {
          technology: 'Jest',
          confidence: 0.80,
          reasoning: 'Jest is the most comprehensive testing framework for JavaScript applications',
          rank: 1
        },
        {
          technology: 'Cypress',
          confidence: 0.75,
          reasoning: 'Cypress provides excellent end-to-end testing capabilities for user workflows',
          rank: 2
        },
        {
          technology: 'Playwright',
          confidence: 0.70,
          reasoning: 'Playwright offers cross-browser testing capabilities for compatibility needs',
          rank: 3
        }
      ],
      ai_ml: [
        {
          technology: 'OpenAI API',
          confidence: 0.60,
          reasoning: 'OpenAI API provides the best AI capabilities for modern applications',
          rank: 1
        },
        {
          technology: 'TensorFlow',
          confidence: 0.55,
          reasoning: 'TensorFlow offers comprehensive ML capabilities for custom AI implementations',
          rank: 2
        },
        {
          technology: 'Hugging Face',
          confidence: 0.50,
          reasoning: 'Hugging Face provides pre-trained models and easy integration for AI needs',
          rank: 3
        }
      ],
      devops: [
        {
          technology: 'Docker',
          confidence: 0.85,
          reasoning: 'Docker is the essential containerization platform for modern DevOps workflows',
          rank: 1
        },
        {
          technology: 'Kubernetes',
          confidence: 0.80,
          reasoning: 'Kubernetes provides orchestration and scaling capabilities for production needs',
          rank: 2
        },
        {
          technology: 'Jenkins',
          confidence: 0.70,
          reasoning: 'Jenkins offers robust CI/CD pipeline capabilities for development workflows',
          rank: 3
        }
      ],
      cloud: [
        {
          technology: 'AWS',
          confidence: 0.80,
          reasoning: 'AWS is the most comprehensive cloud platform for scalable applications',
          rank: 1
        },
        {
          technology: 'Google Cloud',
          confidence: 0.75,
          reasoning: 'Google Cloud offers excellent AI/ML services and competitive pricing',
          rank: 2
        },
        {
          technology: 'Azure',
          confidence: 0.70,
          reasoning: 'Azure provides enterprise integration and Microsoft ecosystem compatibility',
          rank: 3
        }
      ],
      tools: [
        {
          technology: 'Git',
          confidence: 0.90,
          reasoning: 'Git is the essential version control system for all development projects',
          rank: 1
        },
        {
          technology: 'GitHub',
          confidence: 0.85,
          reasoning: 'GitHub provides excellent collaboration features and CI/CD integration',
          rank: 2
        },
        {
          technology: 'GitLab',
          confidence: 0.80,
          reasoning: 'GitLab offers comprehensive DevOps capabilities in a single platform',
          rank: 3
        }
      ]
    };

    // Customize recommendations based on template category
    if (category.includes('ecommerce') || category.includes('marketplace')) {
      baseRecommendations.backend[0].technology = 'Node.js with Stripe';
      baseRecommendations.backend[0].reasoning = 'Node.js with Stripe integration is the optimal choice for e-commerce applications requiring payment processing';
      baseRecommendations.backend[1].technology = 'Python with Django';
      baseRecommendations.backend[1].reasoning = 'Python with Django offers robust e-commerce frameworks and payment processing capabilities';
    }
    
    if (category.includes('healthcare') || category.includes('medical')) {
      baseRecommendations.backend[0].technology = 'Node.js (HIPAA-compliant)';
      baseRecommendations.backend[0].reasoning = 'Node.js with HIPAA compliance is the best backend choice for healthcare applications';
      baseRecommendations.backend[1].technology = 'Python with FastAPI';
      baseRecommendations.backend[1].reasoning = 'Python with FastAPI provides excellent security features for healthcare applications';
    }
    
    if (category.includes('iot') || category.includes('smart')) {
      baseRecommendations.backend[0].technology = 'Node.js with MQTT';
      baseRecommendations.backend[0].reasoning = 'Node.js with MQTT protocol is the optimal choice for IoT applications requiring real-time communication';
      baseRecommendations.backend[1].technology = 'Python with Django';
      baseRecommendations.backend[1].reasoning = 'Python with Django offers excellent IoT data processing capabilities';
    }

    return {
      ...baseRecommendations,
      reasoning: {
        overall: 'These technology options provide comprehensive coverage for this specific template based on its features, business rules, and requirements. The ranked options allow for flexibility in technology selection based on team expertise and project constraints.',
        complexity_assessment: 'medium',
        estimated_development_time: '3-4 months',
        team_size_recommendation: '4-6 developers'
      }
    };
  }
}

module.exports = MockTechStackAnalyzer;
