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

class CompanyStatsController extends GetxController {
  final Dio _dio = DioClient().client;

  var isLoading = false.obs;
  var company = <String, dynamic>{}.obs;
  var totals = <String, dynamic>{}.obs;
  var monthly = <Map<String, dynamic>>[].obs;

  final String companyId;
  final String companyName;

  CompanyStatsController({required this.companyId, required this.companyName});

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

  Future<void> fetchStats() async {
    try {
      isLoading.value = true;
      final response = await _dio.get('companies/stats?company_id=$companyId');
      if (response.data['success'] == true) {
        final data = response.data['data'];
        company.value = Map<String, dynamic>.from(data['company'] ?? {});
        totals.value = Map<String, dynamic>.from(data['totals'] ?? {});
        monthly.value = List<Map<String, dynamic>>.from(data['monthly'] ?? []);
      }
    } catch (e) {
      AppLogger.error('Company stats error', e);
      AppSnackbar.showError('خطأ', 'فشل تحميل الإحصائيات');
    } finally {
      isLoading.value = false;
    }
  }
}
