import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:supabase_flutter/supabase_flutter.dart';

class Storage {
  final SupabaseClient _supabase = Supabase.instance.client;

  Future<bool> createBucket(String name) async {
    try {
      final response = await _supabase.storage.createBucket(name);
      return response.isNotEmpty;
    } catch (e) {
      return false;
    }
  }

  Future<bool> uploadImage(BuildContext context, String storage, Uint8List image, String name) async {
    try {
      final imageBytes = image;
      final imagePath = name;

      await _supabase.storage.from(storage).uploadBinary(imagePath, imageBytes);
      final imageUrl = _supabase.storage.from(storage).getPublicUrl(imagePath);
      return imageUrl.isNotEmpty;
    } catch (e) {
      return false;
    }
  }

  Future<Uint8List> downloadImage(String storage, String name) async {
    try {
      final Uint8List file = await _supabase.storage.from(storage).download(name);
      return file;
    } catch (e) {
      return Uint8List(0);
    }
  }

  Future<String> getImageURL(String storage, String name) async {
    try {
      final String file = _supabase.storage.from(storage).getPublicUrl(name);
      return file;
    } catch (e) {
      return '';
    }
  }
}
