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

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

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

    try {
      final data = await _supabase
          .from('stocks')
          .select('ref_medicines(medicine_name, ref_generic_names(generic_name)), expiration_date, quantity, price')
          .order('ref_medicines(medicine_name)', ascending: true);

      for (var item in data) {
        stockData.add({
          'medicine_name': item['ref_medicines']['medicine_name'],
          'generic_name': item['ref_medicines']['ref_generic_names']['generic_name'],
          'expiration_date': item['expiration_date'],
          'quantity': item['quantity'],
          'price': item['price'],
        });
      }

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

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

    try {
      final data = await _supabase
          .from('stocks')
          .select(
              'ref_medicines_uuid, ref_medicines(medicine_name, ref_generic_names(generic_name)), expiration_date, quantity, price')
          .order('ref_medicines(medicine_name)', ascending: true);

      for (var item in data) {
        stockData.add({
          'uuid': item['ref_medicines_uuid'],
          'medicine_name': item['ref_medicines']['medicine_name'],
          'generic_name': item['ref_medicines']['ref_generic_names']['generic_name'],
          'expiration_date': item['expiration_date'],
          'quantity': item['quantity'],
          'price': item['price'],
        });
      }

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

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

  Future<bool> postStock(String muuid, String name, String quantity, String price) async {
    final uuid = Uuid().v4();

    final stock = {
      'stocks_uuid': uuid,
      'ref_medicines_uuid': muuid,
      'expiration_date': name,
      'quantity': quantity,
      'price': price
    };

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

  Future<String> getQuantity(String uuid) async {
    try {
      final data = await _supabase.from('stocks').select('quantity').eq('stocks_uuid', uuid);
      return data.first['quantity'].toString();
    } catch (e) {
      return '';
    }
  }

  Future<bool> updateStock(String uuid, String column, String value) async {
    try {
      await _supabase.from('stocks').update({column: value}).eq('stocks_uuid', uuid).select();
      return true;
    } catch (e) {
      return false;
    }
  }
}
