import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:hallo_doctor_doctor_app/app/models/withdraw_method_model.dart';
import 'package:hallo_doctor_doctor_app/app/models/bank_withdraw_method_model.dart';
import 'package:hallo_doctor_doctor_app/app/models/withdraw_settings_detail.dart';
import 'package:hallo_doctor_doctor_app/app/services/auth_service.dart';
import 'package:hallo_doctor_doctor_app/app/services/user_service.dart';

class WithdrawService {
  Future<void> addPaypalMethod(String name, String email) async {
    try {
      FirebaseFirestore.instance.collection('WithdrawMethod').add({
        'name': name,
        'email': email,
        'method': 'paypal',
        'userId': UserService().currentUserFirebase!.uid
      });
    } on FirebaseException catch (e) {
      return Future.error(e.message!);
    }
  }

  Future<void> addBankMethod({
    required String name,
    required String accountNumber,
    required String accountHolderName,
    required String bankName,
    String? branchCode,
    String? swiftCode,
  }) async {
    try {
      FirebaseFirestore.instance.collection('WithdrawMethod').add({
        'name': name,
        'accountNumber': accountNumber,
        'accountHolderName': accountHolderName,
        'bankName': bankName,
        'branchCode': branchCode,
        'swiftCode': swiftCode,
        'method': 'bank',
        'userId': UserService().currentUserFirebase!.uid
      });
    } on FirebaseException catch (e) {
      return Future.error(e.message!);
    }
  }

  Future<List<WithdrawMethod>> getWithdrawMethod() async {
    try {
      var withdrawMethodRef = await FirebaseFirestore.instance
          .collection('WithdrawMethod')
          .where(
            'userId',
            isEqualTo: UserService().currentUserFirebase!.uid,
          )
          .get();
      List<WithdrawMethod> listWithdrawMethod =
          withdrawMethodRef.docs.map((doc) {
        var data = doc.data();
        data['withdrawMethodId'] = doc.reference.id;
        if (data['method'] == 'bank') {
          return BankWithdrawMethod.fromJson(data);
        }
        return WithdrawMethod.fromJson(data);
      }).toList();
      if (listWithdrawMethod.isEmpty) return [];
      return listWithdrawMethod;
    } on FirebaseException catch (e) {
      return Future.error(e.message!);
    }
  }

  Future requestWithdraw(
      String password, WithdrawMethod withdrawMethod, int amount) async {
    try {
      bool verifyPassword = await AuthService().verifyPassword(password);
      if (verifyPassword) {
        FirebaseFirestore.instance.collection('WithdrawRequest').add({
          'amount': amount,
          'withdrawMethod': withdrawMethod.toMap(),
          'userId': UserService().currentUserFirebase!.uid
        });
      }
    } catch (e) {
      return Future.error(e.toString());
    }
  }

  Future<WithdrawSettingsDetail> getWithdrawSettings() async {
    try {
      var snapshot = await FirebaseFirestore.instance
          .collection('Settings')
          .doc('withdrawSetting')
          .get();
      WithdrawSettingsDetail withdrawSettingsDetail =
          WithdrawSettingsDetail.fromFirestore(snapshot);
      return withdrawSettingsDetail;
    } catch (e) {
      return Future.error(e.toString());
    }
  }
}
