// routes/admin/supportRoutes.js
const router = require("express").Router();
const { authenticateAdmin, audit } = require("../../middleware");
const { validate, body, query, param } = require("../../middleware/validation");
const supportController = require("../../controllers/admin/supportController");

// ── GET /tickets ────────────────────────────────────────────────────────────
router.get(
  "/tickets",
  authenticateAdmin,
  [
    query("category")
      .optional()
      .isIn(["account", "trading", "deposit", "withdrawal", "verification", "security", "technical", "other"]),
    query("status").optional().isIn(["open", "pending", "in_progress", "resolved", "closed"]),
    query("priority").optional().isIn(["low", "medium", "high", "urgent"]),
    query("search").optional().isString().trim(),
    query("page").optional().isInt({ min: 1 }).toInt(),
    query("limit").optional().isInt({ min: 1, max: 100 }).toInt(),
  ],
  validate,
  supportController.getTickets
);

// ── GET /tickets/stats ───────────────────────────────────────────────────────
router.get("/tickets/stats", authenticateAdmin, supportController.getTicketStats);

// ── GET /tickets/:id ─────────────────────────────────────────────────────────
router.get(
  "/tickets/:id",
  authenticateAdmin,
  [param("id").isUUID()],
  validate,
  supportController.getTicketById
);

// ── POST /tickets ─────────────────────────────────────────────────────────────
router.post(
  "/tickets",
  authenticateAdmin,
  [
    body("category").isIn(["account", "trading", "deposit", "withdrawal", "verification", "security", "technical", "other"]),
    body("subject").trim().isLength({ min: 5, max: 200 }),
    body("description").trim().isLength({ min: 10, max: 5000 }),
    body("priority").optional().isIn(["low", "medium", "high", "urgent"]),
    body("userId").isUUID().withMessage("userId is required"),
    audit("support.create_ticket", "ticket"),
  ],
  validate,
  supportController.createTicket
);

// ── POST /tickets/:id/reply ───────────────────────────────────────────────────
router.post(
  "/tickets/:id/reply",
  authenticateAdmin,
  [
    param("id").isUUID(),
    body("message").trim().isLength({ min: 1, max: 5000 }),
    body("attachments").optional().isArray(),
    body("isInternal").optional().isBoolean(),
    audit("support.reply_ticket", "ticket"),
  ],
  validate,
  supportController.replyToTicket
);

// ── PUT /tickets/:id/close ────────────────────────────────────────────────────
router.put(
  "/tickets/:id/close",
  authenticateAdmin,
  [
    param("id").isUUID(),
    audit("support.close_ticket", "ticket"),
  ],
  validate,
  supportController.closeTicket
);

// ── PUT /tickets/:id/reopen ───────────────────────────────────────────────────
router.put(
  "/tickets/:id/reopen",
  authenticateAdmin,
  [
    param("id").isUUID(),
    audit("support.reopen_ticket", "ticket"),
  ],
  validate,
  supportController.reopenTicket
);

// ── PUT /tickets/:id/status ───────────────────────────────────────────────────
router.put(
  "/tickets/:id/status",
  authenticateAdmin,
  [
    param("id").isUUID(),
    body("status").isIn(["open", "pending", "in_progress", "resolved", "closed"]),
    audit("support.update_ticket_status", "ticket"),
  ],
  validate,
  supportController.updateTicketStatus
);

// ── PUT /tickets/:id/priority ─────────────────────────────────────────────────
router.put(
  "/tickets/:id/priority",
  authenticateAdmin,
  [
    param("id").isUUID(),
    body("priority").isIn(["low", "medium", "high", "urgent"]),
    audit("support.update_ticket_priority", "ticket"),
  ],
  validate,
  supportController.updateTicketPriority
);

// ── DELETE /tickets/:id ───────────────────────────────────────────────────────
router.delete(
  "/tickets/:id",
  authenticateAdmin,
  [
    param("id").isUUID(),
    audit("support.delete_ticket", "ticket"),
  ],
  validate,
  supportController.deleteTicket
);

// ── DELETE /tickets/:id/messages/:msgId ───────────────────────────────────────
router.delete(
  "/tickets/:id/messages/:msgId",
  authenticateAdmin,
  [
    param("id").isUUID(),
    param("msgId").isUUID(),
    audit("support.delete_ticket_message", "ticket_message"),
  ],
  validate,
  supportController.deleteTicketMessage
);

// ── POST /tickets/:id/rate ────────────────────────────────────────────────────
router.post(
  "/tickets/:id/rate",
  authenticateAdmin,
  [
    param("id").isUUID(),
    body("rating").isInt({ min: 1, max: 5 }),
    body("comment").optional().trim().isLength({ max: 500 }),
    audit("support.rate_ticket", "ticket"),
  ],
  validate,
  supportController.rateTicket
);

// ── GET /faq ──────────────────────────────────────────────────────────────────

module.exports = router;