import { Injectable } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';
import { StorageService } from 'src/storage/storage.service';

@Injectable()
export class TempUploadService {
  constructor(
    private prisma: PrismaService,
    private storage: StorageService,
  ) {}

  async saveTempFiles(files: Express.Multer.File[]) {
    return Promise.all(
      files.map(async (file) => {
        // Save to temp folder in storage
        const url = await this.storage.saveTemp(file);

        // Record in DB
        const temp = await this.prisma.tempPhoto.create({
          data: {
            filename: file.originalname,
            url,
            mimetype: file.mimetype,
            size: file.size,
          },
        });

        return { tempId: temp.id, url };
      }),
    );
  }
}