const express = require("express");
const router = express.Router()
const marketData = require("../services/marketDataService");


// Your local trading pairs from DB/config
const tradingPairs = [
  { symbol: "BTC_USDT", type: "spot", name: "Bitcoin" },
  { symbol: "ETH_USDT", type: "spot", name: "Ethereum" },
  { symbol: "BNB_USDT", type: "spot", name: "BNB" },
  { symbol: "XRP_USDT", type: "spot", name: "XRP" },
  { symbol: "EUR_USD", type: "forex", name: "Euro / Dollar" },
];

const streamedSymbols = tradingPairs
  .filter((p) => String(p.type).toLowerCase() === "spot")
  .map((p) => p.symbol);

marketData.start(streamedSymbols);

router.get("/live", (req, res) => {
  const data = tradingPairs.map((pair) => {
    const isCrypto = String(pair.type).toLowerCase() === "spot";
    const symbol = pair.symbol.replaceAll("_", "");
    const ticker = isCrypto ? marketData.getTicker(symbol) : null;

    return {
      ...pair,
      price: ticker?.price ?? 0,
      open: ticker?.open ?? 0,
      high: ticker?.high ?? 0,
      low: ticker?.low ?? 0,
      volume: ticker?.volume ?? 0,
      ticker,
    };
  });

  res.json({
    success: true,
    data,
    updatedAt: Date.now(),
  });
});

router.get("/live/:symbol", (req, res) => {
  const symbol = req.params.symbol;
  const ticker = marketData.getTicker(symbol);

  res.json({
    success: true,
    data: ticker || null,
  });
});

router.get("/candles/current-1m", (req, res) => {
  res.json({
    success: true,
    data: marketData.getCurrent1mCandles(),
  });
});

module.exports = router