export default defineEventHandler(async (event) => {
  if (event.method === 'GET') {
    const objects = await listObjects();
    return objects.map((o) => ({
      name: o.objectKey,
      urn: urnify(o.objectId),
    }));
  }
  
  if (event.method === 'POST') {
    const formData = await readMultipartFormData(event);
    if (!formData) {
      throw createError({
        statusCode: 400,
        statusMessage: `formData should not be ${formData}`,
      }) 
    }

    const file = formData[0];

    try {
      const obj = await uploadObject(file.filename, file.data);
      await translateObject(urnify(obj.objectId), req.fields['model-zip-entrypoint']);

      return {
        name: obj.objectKey,
        urn: urnify(obj.objectId),
      }
    } catch (error) {
      throw createError({
        statusCode: 400,
        statusMessage: 'Upload file failed',
        error,
      }) 
    }
  }
})
