import 'package:supabase_flutter/supabase_flutter.dart';

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

  Future<List> getList() async {
    List<Map<String, dynamic>> medicineData = [];

    try {
      final data = await _supabase
          .from('ref_medicines')
          .select('medicine_name, ref_manufacturers(manufacturer_name)')
          .order('medicine_name', ascending: true);

      for (var item in data) {
        medicineData.add({
          'medicine_name': item['medicine_name'],
          'manufacturer_name': item['ref_manufacturers']['manufacturer_name'],
        });
      }

      return medicineData;
    } catch (e) {
      return [];
    }
  }

  // Future<List> getListAsStocks() async {
  //    List<Map<String, dynamic>> medicineData = [];

  //   try {
  //     final data = await _supabase
  //         .from('ref_medicines')
  //         .select('medicine_name, stocks(manufactorer_name)')
  //         .order('medicine_name', ascending: true);

  //     for (var item in data) {
  //       medicineData.add({
  //         'medicine_name': item['medicine_name'],
  //         'manufacturer_name': item['ref_manufactorers']['manufactorer_name'],
  //       });
  //     }

  //     return medicineData;
  //   } catch (e) {
  //     return [];
  //   }
  // }

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

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

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

  Future<String> getNameUsingBarcode(String barcode) async {
    try {
      final data = await _supabase.from('ref_medicines').select('medicine_name').eq('barcode', barcode);
      return data.first['medicine_name'];
    } catch (e) {
      return '';
    }
  }

  Future<bool> postMedicine(String uuid, String name, String muuid, String guuid, String tuuid, String barcode,
      String distributor, String supplier) async {
    final medicine = {
      'ref_medicines_uuid': uuid,
      'medicine_name': name,
      'ref_manufacturers_uuid': muuid,
      'ref_generic_names_uuid': guuid,
      'ref_types_uuid': tuuid,
      'barcode': barcode,
      'ref_distributors_uuid': distributor,
      'ref_suppliers_uuid': supplier
    };

    try {
      await _supabase.from('ref_medicines').insert(medicine);
      return true;
    } catch (e) {
      return false;
    }
  }
}
