import 'package:supabase_flutter/supabase_flutter.dart';
import 'package:uuid/uuid.dart';

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

  Future<List> getList() async {
    try {
      final data = await _supabase
          .from('ref_manufacturers')
          .select('manufacturer_name')
          .order('manufacturer_name', ascending: true);
      return data.toList();
    } catch (e) {
      return [];
    }
  }

  Future<String> getUUID(String name) async {
    try {
      final data =
          await _supabase.from('ref_manufacturers').select('ref_manufacturers_uuid').eq('manufacturer_name', name);
      return data.first['ref_manufacturers_uuid'].toString();
    } catch (e) {
      return '';
    }
  }

  Future<bool> postManufacturer(String name, String address) async {
    try {
      final genericUUID = Uuid().v4();

      await _supabase
          .from('ref_manufacturers')
          .insert({'ref_manufacturers_uuid': genericUUID, 'manufacturer_name': name, 'manufacturer_address': address});
      return true;
    } catch (e) {
      return false;
    }
  }
}
