import { Router } from "express";

import { authenticate } from "../../middleware/auth.js";
import { validate } from "../../middleware/validate.js";
import { asyncHandler } from "../../utils/http.js";
import { timelineQuerySchema } from "./timeline.schemas.js";
import { getTimeline } from "./timeline.service.js";

export const timelineRouter = Router();

timelineRouter.get(
  "/",
  authenticate,
  validate({ query: timelineQuerySchema }),
  asyncHandler(async (request, response) => {
    const data = await getTimeline({
      from: request.query.from as string | undefined,
      to: request.query.to as string | undefined,
      categoryIds: request.query.categoryIds as string[] | undefined,
      status: request.query.status as "all" | "active" | "service" | "retired" | undefined,
      search: request.query.search as string | undefined,
      customerName: request.query.customerName as string | undefined,
      includeInactive: request.query.includeInactive as boolean | undefined
    });

    response.json(data);
  })
);
