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

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

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

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

  Future<bool> postType(String name) async {
    final typeUUID = Uuid().v4();

    try {
      await _supabase.from('ref_types').insert({'ref_types_uuid': typeUUID, 'type_name': name});
      return true;
    } catch (e) {
      return false;
    }
  }
}
