/**
 * @copyright 2026 NHCarrigan
 * @license Naomi's Public License
 * @author Naomi Carrigan
 */

import { FastifyPluginAsync } from "fastify";
import type { ActivityFeedResponse } from "@library/shared-types";
import { ActivityService } from "../../services/activity.service";

const activityRoutes: FastifyPluginAsync = async (app) => {
  const activityService = new ActivityService();

  /**
   * Get activity feed with optional filters.
   */
  app.get<{
    Querystring: { limit?: number; offset?: number; userId?: string };
    Reply: ActivityFeedResponse;
  }>("/", async (request) => {
    const limit = request.query.limit && request.query.limit > 0
      ? Math.min(request.query.limit, 100)
      : 50;
    const offset = request.query.offset && request.query.offset >= 0
      ? request.query.offset
      : 0;
    const userId = request.query.userId;

    return activityService.getActivityFeed(limit, offset, userId);
  });

  /**
   * Get activity feed for a specific user.
   */
  app.get<{
    Params: { userId: string };
    Querystring: { limit?: number; offset?: number };
    Reply: ActivityFeedResponse;
  }>("/:userId", async (request) => {
    const { userId } = request.params;
    const limit = request.query.limit && request.query.limit > 0
      ? Math.min(request.query.limit, 100)
      : 50;
    const offset = request.query.offset && request.query.offset >= 0
      ? request.query.offset
      : 0;

    return activityService.getActivityFeed(limit, offset, userId);
  });
};

export default activityRoutes;
