import {
  AnyFilesInterceptor,
  FilesInterceptor,
  FileInterceptor,
} from '@nestjs/platform-express';
import { commonStorage } from './file-storage.interceptor';

/**
 * Interceptor for handling ANY number of files from ANY form fields.
 * Use this when the client may upload files with dynamic/unknown field names
 * or when you need to accept files from multiple fields without specifying them individually.
 *
 * @example
 * // In controller:
 * @Post('upload-any')
 * @UseInterceptors(AnyFileUpload())
 * uploadAnyFiles(@UploadedFiles() files: Array<Express.Multer.File>) { ... }
 */
export const AnyFileUpload = () =>
  AnyFilesInterceptor({
    storage: commonStorage, // Uses the shared storage configuration
  });

/**
 * Interceptor for handling MULTIPLE files from a SPECIFIC form field.
 * Use this when you expect multiple files uploaded under the same field name
 * (e.g., image galleries, document collections, file batches).
 *
 * @param fieldName - The form field name that will contain the uploaded files
 * @param maxCount - Maximum number of files allowed (default: 10)
 *
 * @example
 * // In controller:
 * @Post('upload-images')
 * @UseInterceptors(MultiFileUpload('images', 5))
 * uploadImages(@UploadedFiles() files: Array<Express.Multer.File>) { ... }
 */
export const MultiFileUpload = (fieldName: string, maxCount = 10) =>
  FilesInterceptor(fieldName, maxCount, {
    storage: commonStorage, // Uses the shared storage configuration
  });

/**
 * Interceptor for handling a SINGLE file from a SPECIFIC form field.
 * Use this when you expect exactly one file per upload request for a given field
 * (e.g., profile picture, document upload, single file submission).
 *
 * @param fieldName - The form field name that will contain the uploaded file
 *
 * @example
 * // In controller:
 * @Post('upload-avatar')
 * @UseInterceptors(SingleFileUpload('avatar'))
 * uploadAvatar(@UploadedFile() file: Express.Multer.File) { ... }
 */
export const SingleFileUpload = (fieldName: string) =>
  FileInterceptor(fieldName, {
    storage: commonStorage, // Uses the shared storage configuration
  });
