// lib/models/department_category.dart

class DepartmentCategory {
  final String id;
  final String title;

  /// 始终是一个列表，可能为空
  final List<DepartmentCategory> children;

  DepartmentCategory({
    required this.id,
    required this.title,
    List<DepartmentCategory>? children,
  }) : children = children ?? const [];

  factory DepartmentCategory.fromJson(Map<String, dynamic> json) {

    final raw = json['children'];
    final List<DepartmentCategory> parsedChildren = (raw is List)
        ? raw
        .whereType<Map<String, dynamic>>()
        .map((e) => DepartmentCategory.fromJson(e))
        .toList()
        : [];
    return DepartmentCategory(
      id: json['id'] as String,
      title: json['title'] as String,
      children: parsedChildren,
    );
  }

  Map<String, dynamic> toJson() => {
    'id': id,
    'title': title,
    // 始终输出 children 数组，即使为空
    'children': children.map((c) => c.toJson()).toList(),
  };
}
