// File upload endpoint. Reads a multipart form, hands the File to the
// service, returns the public URL the question form will store.

import { NextRequest } from 'next/server';
import { apiHandler, ok } from '@/lib/utils/api';
import { uploadService } from '@/lib/services/upload.service';
import { requireAuth, HttpError } from '@/lib/auth/session';

export async function POST(req: NextRequest) {
  return apiHandler(async () => {
    await requireAuth();
    const form = await req.formData();
    const file = form.get('file');
    if (!(file instanceof File)) {
      throw new HttpError(400, 'No file uploaded');
    }
    const result = await uploadService.saveImage(file);
    return ok(result, 201);
  });
}
