const express = require("express");
const router = express.Router();
const db = require("../models");
const PaginationService = require("../services/PaginationService");
const ReportService = require("../services/ReportService");

// GET /api/v1/order/odd - Return all odd order_id rows
router.get("/odd", async (req, res) => {
  try {
    const oddOrders = await PaginationService.getOddOrders(db.order);
    res.json(oddOrders);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// GET /api/v1/order - Paginate orders with optional sorting
router.get("/", async (req, res) => {
  try {
    const { page = 1, limit = 10, sort = "id", direction = "ASC" } = req.query;

    const result = await PaginationService.paginateWithSort(db.order, {
      page,
      limit,
      sort,
      direction,
    });

    res.json(result);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// GET /api/v1/order/cursor - Cursor-based pagination
router.get("/cursor", async (req, res) => {
  try {
    const { id, limit = 10 } = req.query;

    if (!id) {
      return res.status(400).json({ error: "id parameter is required" });
    }

    const result = await PaginationService.paginateWithCursor(db.order, {
      id,
      limit,
    });

    res.json(result);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

module.exports = router;
