import { getCoverApi, getRateLimiter } from "@/lib/server/app-services";
import { clientAddress, rateLimitResponse } from "@/lib/server/rate-limit";

type Context = { params: Promise<{ bookId: string }> };

export async function GET(_request: Request, context: Context) {
  return getCoverApi().get((await context.params).bookId);
}

export async function PUT(request: Request, context: Context) {
  const limit = await getRateLimiter().consume("cover_upload", clientAddress(request), {
    limit: 20,
    windowSeconds: 60 * 60,
  });
  if (!limit.allowed) return rateLimitResponse(limit.retryAfterSeconds);
  return getCoverApi().put(request, (await context.params).bookId);
}

export async function DELETE(_request: Request, context: Context) {
  return getCoverApi().delete((await context.params).bookId);
}
