import express from 'express';
import { ScannerQuery } from '../queries/scannerQuery.js';
import { ScoringEngine } from '../services/scoringEngine.js';

const router = express.Router();

/**
 * POST /api/scanner/multi-signal
 * Scan for multi-signal opportunities
 */
router.post('/multi-signal', async (req, res, next) => {
  try {
    const criteria = {
      minRocketScore: parseInt(req.body.minRocketScore) || 60,
      minAlignment: parseInt(req.body.minAlignment) || 70,
      minPremium: parseFloat(req.body.minPremium) || 50000,
      symbols: req.body.symbols || null,
      timeWindow: parseInt(req.body.timeWindow) || 60,
      requireBadges: req.body.requireBadges || ['🚀'],
      maxResults: parseInt(req.body.maxResults) || 50
    };
    
    const opportunities = await ScannerQuery.scanMultiSignal(criteria);
    
    // Ensure all flows have rocket scores calculated
    const enriched = opportunities.map(opp => {
      if (!opp.rocketScore && !opp.rocket_score) {
        const rocketScore = ScoringEngine.calculateRocketScore(opp);
        return {
          ...opp,
          rocketScore
        };
      }
      return opp;
    });
    
    res.json({
      success: true,
      count: enriched.length,
      criteria,
      data: enriched
    });
  } catch (error) {
    next(error);
  }
});

/**
 * POST /api/scanner/clusters
 * Scan for institutional clusters
 */
router.post('/clusters', async (req, res, next) => {
  try {
    const criteria = {
      minClusterSize: parseInt(req.body.minClusterSize) || 3,
      timeWindow: parseInt(req.body.timeWindow) || 15,
      minTotalPremium: parseFloat(req.body.minTotalPremium) || 100000,
      symbols: req.body.symbols || null
    };
    
    const clusters = await ScannerQuery.scanClusters(criteria);
    
    res.json({
      success: true,
      count: clusters.length,
      criteria,
      data: clusters
    });
  } catch (error) {
    next(error);
  }
});

/**
 * POST /api/scanner/alert-correlation
 * Scan for alert correlations
 */
router.post('/alert-correlation', async (req, res, next) => {
  try {
    const { alerts } = req.body;
    
    if (!alerts || !Array.isArray(alerts)) {
      return res.status(400).json({
        success: false,
        error: 'Alerts array is required'
      });
    }
    
    const criteria = {
      timeWindow: parseInt(req.body.timeWindow) || 30,
      minConfidence: parseFloat(req.body.minConfidence) || 0.7
    };
    
    const correlations = await ScannerQuery.scanAlertCorrelations(alerts, criteria);
    
    res.json({
      success: true,
      count: correlations.length,
      criteria,
      data: correlations
    });
  } catch (error) {
    next(error);
  }
});

/**
 * GET /api/scanner/opportunities
 * Get top opportunities (quick scan)
 */
router.get('/opportunities', async (req, res, next) => {
  try {
    const limit = parseInt(req.query.limit) || 20;
    const minScore = parseInt(req.query.minScore) || 60;
    
    const opportunities = await ScannerQuery.scanMultiSignal({
      minRocketScore: minScore,
      maxResults: limit,
      timeWindow: 120
    });
    
    res.json({
      success: true,
      count: opportunities.length,
      data: opportunities
    });
  } catch (error) {
    next(error);
  }
});

export default router;

