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

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

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

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

  Future<String> getName(String uuid) async {
    try {
      final data = await _supabase.from('ref_categories').select('category_name').eq('ref_categories_uuid', uuid);
      return data.first['category_name'];
    } catch (e) {
      return '';
    }
  }

  Future<bool> postCategory(String name) async {
    try {
      final categoryUUID = Uuid().v4();
      await _supabase.from('ref_categories').insert({'ref_categories_uuid': categoryUUID, 'category_name': name});
      return true;
    } catch (e) {
      return false;
    }
  }
}
