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

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

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

  Future<String> getCategoryUUID(name) async {
    try {
      final data = await _supabase.from('ref_generic_names').select('ref_categories_uuid').eq('generic_name', name);
      return data.first['ref_categories_uuid'].toString();
    } catch (e) {
      return '';
    }
  }

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

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

      await _supabase
          .from('ref_generic_names')
          .insert({'ref_generic_names_uuid': genericUUID, 'generic_name': name, 'ref_categories_uuid': uuid});
      return true;
    } catch (e) {
      return false;
    }
  }
}
