/// 通用后端响应（对齐 Android R<T>）
class BaseResponse<T> {
  final int code;
  final String? message;
  final T? data;

  const BaseResponse({required this.code, this.message, this.data});

  bool get isSuccess => code == 1;

  factory BaseResponse.fromJson(
    Map<String, dynamic> json,
    T Function(dynamic json) fromData,
  ) {
    return BaseResponse(
      code: json['code'] as int? ?? -1,
      message: json['message'] as String?,
      data: json['data'] == null ? null : fromData(json['data']),
    );
  }
}

/// 分页结果
class PageResult<T> {
  final List<T> items;
  final int total;
  final int page;
  final int size;

  const PageResult({
    required this.items,
    required this.total,
    required this.page,
    required this.size,
  });

  /// 列表别名（与 items 等价）
  List<T> get list => items;

  /// 是否有下一页
  bool get hasMore => items.length >= size && page * size < total;

  factory PageResult.fromJson(
    Map<String, dynamic> json,
    T Function(dynamic json) fromItem,
  ) {
    final list = (json['items'] ?? json['list'] ?? json['data'] ?? []) as List;
    return PageResult<T>(
      items: list.map((e) => fromItem(e)).toList(),
      total: json['total'] as int? ?? 0,
      page: json['page'] as int? ?? 1,
      size: json['size'] as int? ?? 10,
    );
  }
}