import 'package:json_annotation/json_annotation.dart';

part 'pagination_wrapper.g.dart';

@JsonSerializable()
class PageInfo {
  final bool hasNextPage;
  final bool? hasPreviousPage;
  final String? startCursor;
  final String? endCursor;

  PageInfo({
    required this.hasNextPage,
    required this.hasPreviousPage,
    this.startCursor,
    this.endCursor,
  });

  factory PageInfo.fromJson(Map<String, dynamic> json) {
    return _$PageInfoFromJson(json);
  }

  Map<String, dynamic> toJson() => _$PageInfoToJson(this);
}

class Edge<T> {
  final String cursor;
  final T node;

  Edge({
    required this.cursor,
    required this.node,
  });

  factory Edge.fromJson(Map<String, dynamic> json) {
    return Edge(
      cursor: json['cursor'],
      node: json['node'],
    );
  }
}

class PaginationWrapper<T> {
  final PageInfo pageInfo;
  final List<Edge<T>> edges;

  PaginationWrapper({
    required this.pageInfo,
    required this.edges,
  });

  factory PaginationWrapper.fromJson(
      Map<String, dynamic> json, T Function(Map<String, dynamic>)? convertor) {
    return PaginationWrapper(
      pageInfo: PageInfo.fromJson(json['pageInfo']),
      edges: (json['edges'] as List).map((e) {
        return Edge(cursor: e['cursor'], node: convertor?.call(e['node']) as T);
      }).toList(),
    );
  }
}
