"use strict";

const express = require("express");
const router = express.Router();
const signalController = require("../controllers/signalController");
const { authenticate } = require("../middleware/auth");
const { validate, body, param, query } = require("../middleware/validation");

// -------------------------
// User routes
// -------------------------
router.get(
  "/providers",
  authenticate,
  [
    query("page").optional().isInt({ min: 1 }).toInt(),
    query("limit").optional().isInt({ min: 1, max: 100 }).toInt(),
  ],
  validate,
  signalController.getProviders
);

router.get(
  "/me/subscriptions",
  authenticate,
  [
    query("page").optional().isInt({ min: 1 }).toInt(),
    query("limit").optional().isInt({ min: 1, max: 100 }).toInt(),
  ],
  validate,
  signalController.getMySubscriptions
);


router.get(
  "/me/feed",
  authenticate,
  [
    query("page").optional().isInt({ min: 1 }).toInt(),
    query("limit").optional().isInt({ min: 1, max: 100 }).toInt(),
  ],
  validate,
  signalController.getMyFeed
);

router.get(
  "/me/history",
  authenticate,
  [
    query("page").optional().isInt({ min: 1 }).toInt(),
    query("limit").optional().isInt({ min: 1, max: 100 }).toInt(),
  ],
  validate,
  signalController.getMyHistory
);

router.get("/me/stats", authenticate, signalController.getMyStats);

router.post(
  "/providers/:providerId/subscribe",
  authenticate,
  [param("providerId").isInt({ min: 1 }).toInt()],
  validate,
  signalController.subscribe
);

router.post(
  "/providers/:providerId/unsubscribe",
  authenticate,
  [param("providerId").isInt({ min: 1 }).toInt()],
  validate,
  signalController.unsubscribe
);

router.post(
  "/signals/:signalId/buy",
  authenticate,
  [param("signalId").isInt({ min: 1 }).toInt()],
  validate,
  signalController.buySignal
);

router.post(
  "/signals/:signalId/copy",
  authenticate,
  [param("signalId").isInt({ min: 1 }).toInt()],
  validate,
  signalController.copySignal
);

router.post("/me/stop-all", authenticate, signalController.stopAll);

// -------------------------
// Provider routes
// -------------------------
router.get(
  "/provider/signals",
  authenticate,
  [
    query("page").optional().isInt({ min: 1 }).toInt(),
    query("limit").optional().isInt({ min: 1, max: 100 }).toInt(),
    query("status").optional().isIn(["active", "closed", "cancelled", "expired"]),
  ],
  validate,
  signalController.getProviderSignals
);

router.post(
  "/provider/signals",
  authenticate,
  [
    body("pair").trim().notEmpty().isLength({ max: 30 }),
    body("marketType").isIn(["spot", "forex", "stock", "terminal"]),
    body("type").isIn(["buy", "sell", "long", "short"]),
    body("marketEntry").optional().isIn(["market", "limit", "stop"]),
    body("entryPrice").isFloat({ min: 0.00000001 }).toFloat(),
    body("currentPrice").optional().isFloat({ min: 0.00000001 }).toFloat(),
    body("stopLoss").optional({ nullable: true }).isFloat({ min: 0 }).toFloat(),
    body("takeProfit1").optional({ nullable: true }).isFloat({ min: 0 }).toFloat(),
    body("takeProfit2").optional({ nullable: true }).isFloat({ min: 0 }).toFloat(),
    body("takeProfit3").optional({ nullable: true }).isFloat({ min: 0 }).toFloat(),
    body("leverage").optional().isInt({ min: 1 }).toInt(),
    body("timeframe").optional().trim().isLength({ max: 20 }),
    body("analysis").optional().isString(),
    body("imageUrl").optional({ nullable: true }).isString(),
    body("tags").optional().isArray(),
    body("confidence").optional().isIn(["low", "medium", "high"]),
    body("expiresAt").optional({ nullable: true }).isISO8601(),
    body("riskRewardRatio").optional({ nullable: true }).isFloat({ min: 0 }).toFloat(),
  ],
  validate,
  signalController.createSignal
);

router.put(
  "/provider/signals/:signalId",
  authenticate,
  [
    param("signalId").isInt({ min: 1 }).toInt(),
    body("pair").optional().trim().notEmpty().isLength({ max: 30 }),
    body("marketType").optional().isIn(["spot", "forex", "stock", "terminal"]),
    body("type").optional().isIn(["buy", "sell", "long", "short"]),
    body("marketEntry").optional().isIn(["market", "limit", "stop"]),
    body("entryPrice").optional().isFloat({ min: 0.00000001 }).toFloat(),
    body("currentPrice").optional().isFloat({ min: 0.00000001 }).toFloat(),
    body("stopLoss").optional({ nullable: true }).isFloat({ min: 0 }).toFloat(),
    body("takeProfit1").optional({ nullable: true }).isFloat({ min: 0 }).toFloat(),
    body("takeProfit2").optional({ nullable: true }).isFloat({ min: 0 }).toFloat(),
    body("takeProfit3").optional({ nullable: true }).isFloat({ min: 0 }).toFloat(),
    body("leverage").optional().isInt({ min: 1 }).toInt(),
    body("timeframe").optional().trim().isLength({ max: 20 }),
    body("analysis").optional().isString(),
    body("imageUrl").optional({ nullable: true }).isString(),
    body("tags").optional().isArray(),
    body("confidence").optional().isIn(["low", "medium", "high"]),
    body("expiresAt").optional({ nullable: true }).isISO8601(),
    body("riskRewardRatio").optional({ nullable: true }).isFloat({ min: 0 }).toFloat(),
  ],
  validate,
  signalController.updateSignal
);

router.patch(
  "/provider/signals/:signalId/price",
  authenticate,
  [
    param("signalId").isInt({ min: 1 }).toInt(),
    body("currentPrice").isFloat({ min: 0.00000001 }).toFloat(),
  ],
  validate,
  signalController.updateSignalPrice
);

router.post(
  "/provider/signals/:signalId/close",
  authenticate,
  [
    param("signalId").isInt({ min: 1 }).toInt(),
    body("exitPrice").optional().isFloat({ min: 0.00000001 }).toFloat(),
    body("profitLoss").optional().isFloat(),
  ],
  validate,
  signalController.closeSignal
);

router.post(
  "/provider/signals/:signalId/cancel",
  authenticate,
  [param("signalId").isInt({ min: 1 }).toInt()],
  validate,
  signalController.cancelSignal
);

// SSE
router.get("/stream", signalController.stream);

router.post(
  "/place/:signalId/trade",
  authenticate,
  [
    param("signalId").isInt({ min: 1 }).toInt(), 
    body("signalId").isInt({ min: 1 }).toInt()
  ],
  validate,
  signalController.placeSignal
);


module.exports = router;