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

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

  Future<List> getList() async {
    final data = await _supabase.from('ref_medicines').select('medicine_name').order('medicine_name', ascending: true);
    return data.toList();
  }

  Future<List> getList2() async {
    final data = await _supabase
        .from('ref_medicines')
        .select('''medicine_name, ref_manufactorers(manufactorer_name)''').order('medicine_name', ascending: true);
    return data.toList();
  }

  Future<String> getUUID(String name) async {
    final data = await _supabase.from('ref_medicines').select('ref_medicines_uuid').eq('medicine_name', name);
    return data.first['ref_medicines_uuid'];
  }

  Future<void> postMedicine(String name, String muuid, String guuid, String tuuid) async {
    final uuid = Uuid().v4();

    final medicine = {
      'ref_medicines_uuid': uuid,
      'medicine_name': name,
      'ref_manufactorers_uuid': muuid,
      'ref_generic_names_uuid': guuid,
      'ref_types_uuid': tuuid
    };

    await _supabase.from('ref_medicines').insert(medicine);
  }
}
