const express = require('express');
const router = express.Router();
const livePriceController = require('../controllers/livePriceController');
const { validate, validateQuery, validateParams, livePriceSchema } = require('../middleware/validation');
const Joi = require('joi');

// GET /api/live-prices - Get all live prices
router.get('/', validateQuery(Joi.object({
  limit: Joi.number().integer().min(1).max(1000).default(100),
  offset: Joi.number().integer().min(0).default(0)
})), livePriceController.getAllLivePrices);

// GET /api/live-prices/exchange/:exchange - Get live prices by exchange
router.get('/exchange/:exchange', validateParams(Joi.object({
  exchange: Joi.string().max(50).required()
})), validateQuery(Joi.object({
  limit: Joi.number().integer().min(1).max(1000).default(100),
  offset: Joi.number().integer().min(0).default(0)
})), livePriceController.getLivePricesByExchange);

// GET /api/live-prices/type/:type - Get live prices by instrument type
router.get('/type/:type', validateParams(Joi.object({
  type: Joi.string().valid('crypto', 'stock', 'forex', 'commodity').required()
})), validateQuery(Joi.object({
  limit: Joi.number().integer().min(1).max(1000).default(100),
  offset: Joi.number().integer().min(0).default(0)
})), livePriceController.getLivePricesByType);

// GET /api/live-prices/:symbolId - Get live price for a specific symbol
router.get('/:symbolId', validateParams(Joi.object({
  symbolId: Joi.number().integer().positive().required()
})), livePriceController.getLivePrice);

// POST /api/live-prices - Create or update live price
router.post('/', validate(livePriceSchema), livePriceController.upsertLivePrice);

// POST /api/live-prices/bulk - Bulk update live prices
router.post('/bulk', validate(Joi.object({
  prices: Joi.array().items(livePriceSchema).min(1).required()
})), livePriceController.bulkUpdateLivePrices);

// DELETE /api/live-prices/:symbolId - Delete live price for a symbol
router.delete('/:symbolId', validateParams(Joi.object({
  symbolId: Joi.number().integer().positive().required()
})), livePriceController.deleteLivePrice);

module.exports = router;
