enum FinanceType { receivable, payable }

extension FinanceTypeX on FinanceType {
  String get value => name;
  String get label => this == FinanceType.receivable ? 'Alacak' : 'Borç';
}

enum FinanceStatus { pending, paid }

extension FinanceStatusX on FinanceStatus {
  String get value => name;
  String get label => this == FinanceStatus.pending ? 'Bekliyor' : 'Ödendi';
}

class FinanceEntry {
  const FinanceEntry({
    required this.id,
    required this.tenantId,
    required this.jobId,
    required this.type,
    required this.amount,
    required this.currency,
    required this.status,
    this.paidAt,
    this.counterpartyName,
    this.patientCode,
    this.dateCreated,
  });

  final String id;
  final String tenantId;
  final String jobId;
  final FinanceType type;
  final double amount;
  final String currency;
  final FinanceStatus status;
  final String? paidAt;
  final String? counterpartyName;
  final String? patientCode;
  final String? dateCreated;

  factory FinanceEntry.fromJson(Map<String, dynamic> j) {
    final expand = j['expand'] as Map<String, dynamic>?;
    final jobExp = expand?['job_id'] as Map<String, dynamic>?;
    String? _str(dynamic v) { final s = v as String?; return (s == null || s.isEmpty) ? null : s; }
    return FinanceEntry(
      id: j['id'] as String,
      tenantId: j['tenant_id'] as String,
      jobId: j['job_id'] as String,
      type: FinanceType.values.firstWhere((e) => e.value == j['type'],
          orElse: () => FinanceType.receivable),
      amount: (j['amount'] as num).toDouble(),
      currency: j['currency'] as String? ?? 'TRY',
      status: FinanceStatus.values.firstWhere((e) => e.value == j['status'],
          orElse: () => FinanceStatus.pending),
      paidAt: _str(j['paid_at']),
      counterpartyName: _str(j['counterparty_name']),
      patientCode: jobExp?['patient_code'] as String?,
      dateCreated: j['created'] as String?,
    );
  }
}
