import 'dart:convert';
import 'package:get/get.dart';
import 'package:siro_admin/constant/links.dart';
import 'package:siro_admin/controller/functions/crud.dart';
import '../../print.dart';

class FinancialV2Controller extends GetxController {
  bool isLoading = true;

  Map<String, dynamic> stats = {};
  List<dynamic> settlements = [];

  @override
  void onInit() {
    super.onInit();
    fetchAllFinancials();
  }

  Future<void> fetchAllFinancials() async {
    isLoading = true;
    update();

    await Future.wait([
      fetchStats(),
      fetchSettlements(),
    ]);

    isLoading = false;
    update();
  }

  Future<void> fetchStats() async {
    try {
      var res =
          await CRUD().getWallet(link: AppLink.financialStatsV2, payload: {});
      if (res != 'failure' && res != null) {
        var d = res is String ? jsonDecode(res) : res;
        if (d['status'] == 'success') {
          stats = d['data'];
        }
      }
    } catch (e) {
      Log.print('Error fetching financial stats: $e');
    }
  }

  Future<void> fetchSettlements() async {
    try {
      var res =
          await CRUD().getWallet(link: AppLink.settlementsV2, payload: {});
      if (res != 'failure' && res != null) {
        var d = res is String ? jsonDecode(res) : res;
        if (d['status'] == 'success') {
          settlements = d['data'];
        }
      }
    } catch (e) {
      Log.print('Error fetching settlements: $e');
    }
  }
}
