import { NextResponse } from 'next/server';

import { requireSuperAdmin, withAuth } from '@/lib/api/helpers';
import { errorResponse, NotFoundError } from '@/lib/errors';
import { getBackupDownloadUrl } from '@/lib/services/backup.service';

export const GET = withAuth(async (_req, ctx, params) => {
  try {
    requireSuperAdmin(ctx, 'admin.backup.download');
    const id = params.id;
    if (!id) throw new NotFoundError('Backup');
    const url = await getBackupDownloadUrl(id);
    if (!url) throw new NotFoundError('Backup');
    return NextResponse.json({ data: { url } });
  } catch (error) {
    return errorResponse(error);
  }
});
