class Patient {
  const Patient({
    required this.id,
    required this.tenantId,
    required this.patientCode,
    this.firstName,
    this.lastName,
    this.birthDate,
    this.phone,
    this.notes,
  });

  final String id;
  final String tenantId;
  final String patientCode;
  final String? firstName;
  final String? lastName;
  final String? birthDate;
  final String? phone;
  final String? notes;

  String get displayName {
    final parts = [firstName, lastName].where((s) => s != null && s.isNotEmpty);
    return parts.isEmpty ? patientCode : parts.join(' ');
  }

  factory Patient.fromJson(Map<String, dynamic> j) => Patient(
        id: j['id'] as String,
        tenantId: j['tenant_id'] is Map
            ? (j['tenant_id'] as Map)['id'] as String
            : j['tenant_id'] as String,
        patientCode: j['patient_code'] as String,
        firstName: j['first_name'] as String?,
        lastName: j['last_name'] as String?,
        birthDate: j['birth_date'] as String?,
        phone: j['phone'] as String?,
        notes: j['notes'] as String?,
      );

  Map<String, dynamic> toJson() => {
        'tenant_id': tenantId,
        'patient_code': patientCode,
        if (firstName != null) 'first_name': firstName,
        if (lastName != null) 'last_name': lastName,
        if (birthDate != null) 'birth_date': birthDate,
        if (phone != null) 'phone': phone,
        if (notes != null) 'notes': notes,
      };
}
