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

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

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

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

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

      await _supabase.from('ref_suppliers').insert({'ref_suppliers_uuid': genericUUID, 'supplier_name': name});
      return true;
    } catch (e) {
      return false;
    }
  }
}
