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

class InvoicesController extends GetxController {
  var invoices = <Map<String, dynamic>>[].obs;
  var isLoading = true.obs;
  var filterStatus = 'all'.obs;
  var searchQuery = ''.obs;
  var isSearching = false.obs;

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

  List<Map<String, dynamic>> get filteredInvoices {
    var list = invoices.toList();

    if (filterStatus.value != 'all') {
      list = list.where((inv) => inv['status'] == filterStatus.value).toList();
    }

    if (searchQuery.value.isNotEmpty) {
      final q = searchQuery.value.toLowerCase();
      list = list.where((inv) {
        final name = (inv['supplier_name'] ?? '').toString().toLowerCase();
        final company = (inv['company_name'] ?? '').toString().toLowerCase();
        final num = (inv['invoice_number'] ?? '').toString().toLowerCase();
        return name.contains(q) || company.contains(q) || num.contains(q);
      }).toList();
    }

    return list;
  }

  void applyVoiceFilters(Map<String, dynamic> params) {
    filterStatus.value = _normalizeStatus(params['status']);

    final query =
        (params['number'] ?? params['company'] ?? '').toString().trim();
    searchQuery.value = query;
    isSearching.value = query.isNotEmpty;
  }

  void toggleSearch() {
    isSearching.value = !isSearching.value;
    if (!isSearching.value) searchQuery.value = '';
  }

  Future<void> loadInvoices() async {
    try {
      isLoading.value = true;
      final res = await DioClient().client.get('invoices');
      if (res.data['success'] == true && res.data['data'] != null) {
        invoices.value = List<Map<String, dynamic>>.from(res.data['data']);
      }
    } catch (e) {
      AppLogger.error('Failed to load invoices', e);
    } finally {
      isLoading.value = false;
    }
  }

  String _normalizeStatus(dynamic status) {
    final value = status?.toString().toLowerCase().trim() ?? '';
    final aliases = {
      'ready': 'extracted',
      'جاهزة': 'extracted',
      'pending': 'uploaded',
      'processing': 'uploaded',
      'قيد المعالجة': 'uploaded',
      'معتمدة': 'approved',
    };
    final normalized = aliases[value] ?? value;

    const supported = {
      'all',
      'approved',
      'extracted',
      'uploaded',
      'submitted',
      'rejected',
    };

    return supported.contains(normalized) ? normalized : 'all';
  }
}
