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

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

  Future<List> getSettings(String user) async {
    try {
      final data = await _supabase.from('settings').select('language, name').eq('user_id', user);
      return data.toList();
    } catch (e) {
      return [];
    }
  }

  Future<bool> postSettings(String user, String language, String name) async {
    final uuid = Uuid().v4();

    final setting = {
      'settings_uuid': uuid,
      'user_id': user,
      'language': language,
      'name': name,
    };

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

  Future<bool> updateLanguage(String user, String language) async {
    try {
      await _supabase.from('settings').update({'language': language}).eq('user_id', user).select();
      return true;
    } catch (e) {
      return false;
    }
  }

  Future<bool> updateName(String user, String name) async {
    try {
      await _supabase.from('settings').update({'name': name}).eq('user_id', user).select();
      return true;
    } catch (e) {
      return false;
    }
  }

  Future<bool> checkExist(String uuid) async {
    try {
      final data = await _supabase.from('settings').select().eq('user_id', uuid);
      return data.isNotEmpty;
    } catch (e) {
      return false;
    }
  }
}
