import 'dart:io';

import 'package:firebase_storage/firebase_storage.dart';

class ImageService {
  Future<String> uploadImageToStorage(File? file, String carId) async {
    if (file == null) {
      print('Error uploading image to Firebase Storage: Image is null');
      return '';
    }
    try {
      String fileName = '${carId}_car.jpg';
      Reference storageReference =
          FirebaseStorage.instance.ref().child('car_images/$fileName');
      await storageReference.putFile(file);
      String downloadUrl = await storageReference.getDownloadURL();
      return downloadUrl;
    } catch (e) {
      print('Error uploading image to Firebase Storage: $e');
      return '';
    }
  }

  Future<void> deleteImage(String photoUrl) async {
    try {
      Reference storageReference =
          FirebaseStorage.instance.refFromURL(photoUrl);
      await storageReference.delete();
    } catch (e) {
      print('Error uploading image to Firebase Storage: $e');
      return;
    }
  }
}
