import 'package:get/get.dart';
import '../../../core/network/dio_client.dart';
import '../../../core/utils/logger.dart';

class AiUsageController extends GetxController {
  var isLoading = true.obs;
  var data = Rxn<Map<String, dynamic>>();

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

  Map<String, dynamic> get overall => Map<String, dynamic>.from(data.value?['overall'] ?? {});
  Map<String, dynamic> get today => Map<String, dynamic>.from(data.value?['today'] ?? {});
  Map<String, dynamic> get thisMonth => Map<String, dynamic>.from(data.value?['this_month'] ?? {});
  List get dailyBreakdown => data.value?['daily_breakdown'] ?? [];

  Future<void> fetchUsage() async {
    try {
      isLoading.value = true;
      final res = await DioClient().client.get('dashboard/ai-usage');
      if (res.data['success'] == true) {
        data.value = Map<String, dynamic>.from(res.data['data']);
      }
    } catch (e) {
      AppLogger.error('Failed to fetch AI usage', e);
    } finally {
      isLoading.value = false;
    }
  }
}
