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

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

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

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

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

      await _supabase
          .from('ref_distributors')
          .insert({'ref_distributors_uuid': genericUUID, 'distributor_name': name, 'distributor_address': address});
      return true;
    } catch (e) {
      return false;
    }
  }
}
