require('dotenv').config();
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const morgan = require('morgan');
const http = require('http');
const { Server } = require('socket.io');
const axios = require('axios');

// Import database
const database = require('./config/database');

// Import routes (we'll create these next)
const templateRoutes = require('./routes/templates');
const featureRoutes = require('./routes/features');
const learningRoutes = require('./routes/learning');
const adminRoutes = require('./routes/admin');
const adminTemplateRoutes = require('./routes/admin-templates');
const AdminNotification = require('./models/admin_notification');
// const customTemplateRoutes = require('./routes/custom_templates');

const app = express();
const server = http.createServer(app);
const io = new Server(server, {
  cors: {
    origin: "*",
    methods: ["GET", "POST"],
    credentials: true
  }
});
const PORT = process.env.PORT || 8009;

// Middleware
app.use(helmet());
app.use(cors({
  origin: "*",
  credentials: true,
  methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
  allowedHeaders: ['Content-Type', 'Authorization', 'X-User-ID', 'X-User-Role']
}));
app.use(morgan('combined'));
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true }));

// Make io available to routes and set it in AdminNotification
app.set('io', io);
AdminNotification.setSocketIO(io);

// Routes - Order matters! More specific routes should come first
app.use('/api/learning', learningRoutes);
app.use('/api/admin', adminRoutes);
app.use('/api/admin/templates', adminTemplateRoutes);
app.use('/api/templates', templateRoutes);
// Add admin routes under /api/templates to match serviceClient expectations
app.use('/api/templates/admin', adminRoutes);
// Features route must come AFTER templates to avoid route conflicts
app.use('/api/features', featureRoutes);
// Single route surface: handle custom templates via /api/templates only
// app.use('/api/custom-templates', customTemplateRoutes);

// WebSocket connection handling
io.on('connection', async (socket) => {
  console.log('🔌 Admin client connected:', socket.id);
  
  // Join admin room for notifications
  socket.join('admin-notifications');
  
  // Send initial notification count
  try {
    const counts = await AdminNotification.getCounts();
    socket.emit('notification-count', counts);
  } catch (error) {
    console.error('Error getting notification counts:', error);
    socket.emit('notification-count', { total: 0, unread: 0, read: 0 });
  }
  
  socket.on('disconnect', () => {
    console.log('🔌 Admin client disconnected:', socket.id);
  });
});

// Health check endpoint
app.get('/health', (req, res) => {
  res.status(200).json({
    status: 'healthy',
    service: 'template-manager',
    version: '1.0.0',
    timestamp: new Date().toISOString(),
    uptime: process.uptime(),
    features: {
      template_management: true,
      feature_learning: true,
      usage_tracking: true,
      self_improving: true,
      admin_approval_workflow: true,
      ai_analysis: true
    }
  });
});

// AI Feature Analysis endpoint
app.post('/api/analyze-feature', async (req, res) => {
  try {
    console.log('🤖 [Template Manager] AI Analysis request received');
    
    const { featureName, feature_name, description, requirements = [], projectType, project_type } = req.body;
    
    // Handle both parameter name variations
    const actualFeatureName = featureName || feature_name;
    const actualProjectType = projectType || project_type;
    
    // Ensure requirements is always an array
    const safeRequirements = Array.isArray(requirements) ? requirements : [];
    
    console.log('📋 [Template Manager] Analyzing feature:', actualFeatureName);
    console.log('📋 [Template Manager] Project type:', actualProjectType);
    console.log('📋 [Template Manager] Requirements:', safeRequirements);
    
    // Use Claude AI for intelligent analysis
    let analysis;
    const CLAUDE_API_KEY = process.env.CLAUDE_API_KEY || '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';
    const CLAUDE_AVAILABLE = !!CLAUDE_API_KEY;
    
    console.log('🔍 [Template Manager] Claude available:', CLAUDE_AVAILABLE);
    console.log('🔍 [Template Manager] Claude API key present:', !!process.env.CLAUDE_API_KEY);
    
    if (CLAUDE_AVAILABLE) {
      try {
        console.log('🤖 [Template Manager] Using Claude AI for analysis...');
        analysis = await analyzeWithClaude(actualFeatureName, description, safeRequirements, actualProjectType);
        console.log('✅ [Template Manager] Claude AI analysis completed successfully');
      } catch (claudeError) {
        console.warn('⚠️ [Template Manager] Claude AI failed, falling back to rule-based analysis:', claudeError.message);
        console.error('❌ [Template Manager] Claude error details:', claudeError);
        analysis = await analyzeWithRules(actualFeatureName, description, safeRequirements, actualProjectType);
      }
    } else {
      console.log('📋 [Template Manager] Using rule-based analysis (Claude not available)');
      analysis = await analyzeWithRules(actualFeatureName, description, safeRequirements, actualProjectType);
    }
    
    console.log('✅ [Template Manager] Analysis completed:', analysis.complexity, 'complexity');
    
    res.json({
      success: true,
      analysis: analysis
    });
    
  } catch (error) {
    console.error('❌ [Template Manager] AI Analysis error:', error);
    res.status(500).json({
      success: false,
      error: error.message,
      message: 'AI analysis failed'
    });
  }
});

// Claude AI Analysis function
async function analyzeWithClaude(featureName, description, requirements, projectType) {
  const CLAUDE_API_KEY = process.env.CLAUDE_API_KEY || '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';
  
  const safeRequirements = Array.isArray(requirements) ? requirements : [];
  const requirementsText = safeRequirements.length > 0 ? safeRequirements.map(req => `- ${req}`).join('\n') : 'No specific requirements provided';
  
  const prompt = `Analyze this custom feature for a ${projectType || 'web application'} project:

Feature Name: ${featureName || 'Custom Feature'}
Description: ${description || 'No description provided'}

Detailed Requirements:
${requirementsText}

Based on these requirements, provide a detailed analysis in JSON format:
{
  "feature_name": "Improved technical name",
  "complexity": "low|medium|high",
  "logicRules": ["Business rule 1", "Business rule 2", "Business rule 3"],
  "implementation_details": ["Technical detail 1", "Technical detail 2", "Technical detail 3"],
  "technical_requirements": ["Requirement 1", "Requirement 2", "Requirement 3"], 
  "estimated_effort": "1-2 weeks|2-3 weeks|3-4 weeks|4+ weeks",
  "dependencies": ["Dependency 1", "Dependency 2"],
  "api_endpoints": ["POST /api/endpoint1", "GET /api/endpoint2"],
  "database_tables": ["table1", "table2"],
  "confidence_score": 0.85
}

For complexity assessment:
- "low": Simple CRUD, basic features, minimal business logic
- "medium": Moderate business logic, some integrations, standard features
- "high": Complex business rules, external integrations, security requirements, real-time features

For logicRules, generate specific business rules based on the requirements like:
- Access control and authorization rules
- Data validation and business logic rules  
- Workflow and process rules
- Security and compliance requirements

Return ONLY the JSON object, no other text.`;

  try {
    console.log('🔍 [Template Manager] Making Claude API request...');
    console.log('🔍 [Template Manager] API Key length:', CLAUDE_API_KEY ? CLAUDE_API_KEY.length : 0);
    console.log('🔍 [Template Manager] Prompt length:', prompt.length);
    
    const response = await axios.post('https://api.anthropic.com/v1/messages', {
      model: 'claude-3-5-sonnet-20241022',
      max_tokens: 2000,
      temperature: 0.1,
      messages: [{ role: 'user', content: prompt }]
    }, {
      headers: {
        'x-api-key': CLAUDE_API_KEY,
        'Content-Type': 'application/json',
        'anthropic-version': '2023-06-01'
      },
      timeout: 30000
    });

    console.log('✅ [Template Manager] Claude API response received');
    console.log('🔍 [Template Manager] Response status:', response.status);
    
    const responseText = response.data.content[0].text.trim();
    console.log('🔍 [Template Manager] Raw Claude response:', responseText.substring(0, 200) + '...');
    
    // Extract JSON from response
    const jsonMatch = responseText.match(/\{[\s\S]*\}/);
    if (jsonMatch) {
      const analysis = JSON.parse(jsonMatch[0]);
      console.log('✅ [Template Manager] Claude analysis successful');
      console.log('🔍 [Template Manager] Parsed analysis:', JSON.stringify(analysis, null, 2));
      return analysis;
    } else {
      console.error('❌ [Template Manager] No valid JSON found in Claude response');
      console.error('🔍 [Template Manager] Full response:', responseText);
      throw new Error('No valid JSON found in Claude response');
    }
  } catch (error) {
    console.error('❌ [Template Manager] Claude API error:', error.message);
    console.error('🔍 [Template Manager] Error details:', {
      status: error.response?.status,
      statusText: error.response?.statusText,
      data: error.response?.data,
      code: error.code
    });
    throw error;
  }
}

// Rule-based analysis function (fallback)
async function analyzeWithRules(featureName, description, requirements, projectType) {
  const complexity = analyzeComplexity(description, requirements);
  const logicRules = generateLogicRules(featureName, description, requirements, projectType);
  
  return {
    feature_name: featureName || 'Custom Feature',
    complexity: complexity,
    logicRules: logicRules,
    implementation_details: [
      `Implement ${featureName || 'Custom Feature'} with proper validation`,
      'Add error handling and logging',
      'Include unit and integration tests'
    ],
    technical_requirements: [
      'Database schema design',
      'API endpoint implementation', 
      'Frontend component development'
    ],
    estimated_effort: complexity === 'high' ? '3-4 weeks' : complexity === 'low' ? '1-2 weeks' : '2-3 weeks',
    dependencies: ['User authentication', 'Database setup'],
    api_endpoints: [
      `POST /api/${(featureName || 'custom-feature').toLowerCase().replace(/\s+/g, '-')}`,
      `GET /api/${(featureName || 'custom-feature').toLowerCase().replace(/\s+/g, '-')}`
    ],
    database_tables: [`${(featureName || 'custom_feature').toLowerCase().replace(/\s+/g, '_')}_table`],
    confidence_score: 0.75
  };
}

// Helper function to analyze complexity
function analyzeComplexity(description, requirements) {
  const safeRequirements = Array.isArray(requirements) ? requirements : [];
  const text = `${description || ''} ${safeRequirements.join(' ')}`.toLowerCase();
  
  const highComplexityKeywords = ['encryption', 'hipaa', 'compliance', 'security', 'integration', 'real-time', 'ai', 'machine learning', 'blockchain', 'payment', 'transaction'];
  const mediumComplexityKeywords = ['crud', 'database', 'api', 'authentication', 'validation', 'search', 'filter', 'workflow', 'approval'];
  const lowComplexityKeywords = ['display', 'show', 'view', 'list', 'basic', 'simple'];
  
  if (highComplexityKeywords.some(keyword => text.includes(keyword))) {
    return 'high';
  } else if (mediumComplexityKeywords.some(keyword => text.includes(keyword))) {
    return 'medium';
  } else if (lowComplexityKeywords.some(keyword => text.includes(keyword))) {
    return 'low';
  }
  
  return 'medium'; // default
}

// Helper function to generate logic rules
function generateLogicRules(featureName, description, requirements, projectType) {
  const rules = [];
  const safeRequirements = Array.isArray(requirements) ? requirements : [];
  const text = `${description || ''} ${safeRequirements.join(' ')}`.toLowerCase();
  const featureText = `${featureName || ''}`.toLowerCase();
  
  console.log('🔍 [Template Manager] Generating rules for:', featureName);
  console.log('🔍 [Template Manager] Description:', description);
  console.log('🔍 [Template Manager] Requirements:', safeRequirements);
  console.log('🔍 [Template Manager] Project type:', projectType);
  
  // Project type specific rules
  if (projectType?.toLowerCase() === 'healthcare') {
    rules.push('Only authorized caregivers can access patient data');
    rules.push('All patient data access must be logged for HIPAA compliance');
    rules.push('Patient data must be encrypted at rest and in transit');
  }
  
  if (projectType?.toLowerCase() === 'ecommerce') {
    rules.push('Payment information must be PCI DSS compliant');
    rules.push('Order status updates must be real-time');
    rules.push('Inventory levels must be validated before purchase');
  }
  
  if (projectType?.toLowerCase() === 'finance' || projectType?.toLowerCase() === 'fintech') {
    rules.push('All financial data must be encrypted and access-controlled');
    rules.push('Transaction processing must include fraud detection');
    rules.push('Audit trails must be maintained for all financial operations');
  }
  
  // Dynamic feature-specific rules based on content analysis
  if (text.includes('user') || text.includes('account') || text.includes('profile')) {
    rules.push(`User authentication is required to access ${featureName || 'this feature'}`);
    rules.push('User data must be validated before storage');
    rules.push('Users can only access their own data unless explicitly authorized');
  }
  
  if (text.includes('crud') || text.includes('manage') || text.includes('create') || text.includes('edit') || text.includes('delete')) {
    rules.push(`Only authorized users can perform ${featureName || 'data'} operations`);
    rules.push('All data modifications must be validated and sanitized');
    rules.push('Delete operations should be soft deletes with audit trails');
  }
  
  if (text.includes('search') || text.includes('filter') || text.includes('query')) {
    rules.push('Search queries must be sanitized to prevent injection attacks');
    rules.push('Search results must respect user permissions and data visibility');
    rules.push('Search performance should be optimized with proper indexing');
  }
  
  if (text.includes('upload') || text.includes('file') || text.includes('document')) {
    rules.push('File uploads must be validated for type, size, and content');
    rules.push('Uploaded files must be scanned for malware');
    rules.push('File access must be controlled based on user permissions');
  }
  
  if (text.includes('notification') || text.includes('email') || text.includes('alert')) {
    rules.push('Notification preferences must be configurable by users');
    rules.push('Email notifications must comply with anti-spam regulations');
    rules.push('Notification delivery must be reliable and trackable');
  }
  
  if (text.includes('report') || text.includes('analytics') || text.includes('dashboard')) {
    rules.push('Report data must be filtered based on user access permissions');
    rules.push('Analytics data must be anonymized where required');
    rules.push('Report generation must be optimized for performance');
  }
  
  if (text.includes('api') || text.includes('integration') || text.includes('webhook')) {
    rules.push('API endpoints must be secured with proper authentication');
    rules.push('Rate limiting must be implemented to prevent abuse');
    rules.push('API responses must not expose sensitive internal data');
  }
  
  if (text.includes('payment') || text.includes('transaction') || text.includes('billing')) {
    rules.push('All financial transactions must be logged and auditable');
    rules.push('Payment processing must include fraud detection');
    rules.push('Transaction data must be encrypted and PCI compliant');
  }
  
  if (text.includes('approval') || text.includes('workflow') || text.includes('review')) {
    rules.push('Approval workflows must have configurable escalation rules');
    rules.push('All approval decisions must be logged with timestamps');
    rules.push('Workflow states must be clearly defined and trackable');
  }
  
  if (text.includes('patient') || text.includes('medical') || text.includes('health')) {
    rules.push('Patient information can only be accessed by assigned caregivers');
    rules.push('All patient data modifications require audit trail');
    rules.push('Medical data must comply with HIPAA regulations');
  }
  
  // Dynamic rules based on requirements content
  safeRequirements.forEach((req, index) => {
    const reqText = req.toLowerCase();
    if (reqText.includes('security') || reqText.includes('secure')) {
      rules.push(`Security requirement ${index + 1}: ${req} must be implemented with proper access controls`);
    }
    if (reqText.includes('performance') || reqText.includes('fast') || reqText.includes('speed')) {
      rules.push(`Performance requirement ${index + 1}: ${req} must be optimized for scalability`);
    }
    if (reqText.includes('compliance') || reqText.includes('regulation')) {
      rules.push(`Compliance requirement ${index + 1}: ${req} must be implemented according to regulatory standards`);
    }
  });
  
  // Generic rules if no specific ones were added
  if (rules.length === 0) {
    rules.push(`${featureName || 'This feature'} must implement proper data validation on all inputs`);
    rules.push(`User permissions must be verified before accessing ${featureName || 'feature'} functionality`);
    rules.push(`All ${featureName || 'feature'} operations must be logged for audit and debugging purposes`);
    rules.push(`${featureName || 'This feature'} must handle errors gracefully and provide meaningful feedback`);
  }
  
  // Ensure we always have at least 3-5 rules for better analysis
  if (rules.length < 3) {
    rules.push(`${featureName || 'This feature'} must be tested thoroughly before deployment`);
    rules.push(`${featureName || 'This feature'} must follow established coding standards and best practices`);
  }
  
  console.log('✅ [Template Manager] Generated rules:', rules);
  return rules;
}

// Root endpoint
app.get('/', (req, res) => {
  res.json({
    message: 'Template Manager Service - Self-Learning Feature Database',
    version: '1.0.0',
    endpoints: {
      health: '/health',
      templates: '/api/templates',
      features: '/api/features',
      learning: '/api/learning',
      admin: '/api/admin',
      customTemplates: '/api/custom-templates'
    }
  });
});

// Error handling middleware
app.use((err, req, res, next) => {
  console.error('❌ Error:', err.stack);
  res.status(500).json({
    error: 'Internal Server Error',
    message: process.env.NODE_ENV === 'development' ? err.message : 'Something went wrong'
  });
});

// 404 handler
app.use('*', (req, res) => {
  res.status(404).json({
    error: 'Not Found',
    message: `Route ${req.originalUrl} not found`
  });
});

// Graceful shutdown
process.on('SIGINT', async () => {
  console.log('🛑 Shutting down Template Manager...');
  await database.close();
  process.exit(0);
});

// Start server
server.listen(PORT, '0.0.0.0', () => {
  console.log('🚀 Template Manager Service started');
  console.log(`📡 Server running on http://0.0.0.0:${PORT}`);
  console.log(`🏥 Health check: http://0.0.0.0:${PORT}/health`);
  console.log('🔌 WebSocket server ready for real-time notifications');
  console.log('🎯 Self-learning feature database ready!');
});

module.exports = app;