import { Type } from '@nestjs/common';
import { ArgsType, Field, Int, ObjectType } from '@nestjs/graphql';

@ArgsType()
export class PaginationArgs {
  @Field(() => Int)
  take = 10;
  @Field(() => String, { nullable: true })
  cursor?: string;
}

// interface IEdgeType<T> {
//   cursor: string;
//   node: T;
// }

export interface IPaginatedType<T> {
  // edges: IEdgeType<T>[];
  nodes: T[];
  prevCursor: string;
  endCursor: string;
  totalCount: number;
  hasNextPage: boolean;
}

export function Paginated<T>(classRef: Type<T>): Type<IPaginatedType<T>> {
  // @ObjectType(`${classRef.name}Edge`)
  // abstract class EdgeType {
  //   @Field(() => String)
  //   cursor: string;
  //   @Field(() => classRef)
  //   node: T;
  // }

  @ObjectType({ isAbstract: true })
  abstract class PaginatedType implements IPaginatedType<T> {
    // @Field(() => [EdgeType], { nullable: true })
    // edges: EdgeType[];

    @Field(() => [classRef])
    nodes: T[];

    @Field(() => String)
    prevCursor: string;

    @Field(() => String)
    endCursor: string;

    @Field(() => Int)
    totalCount: number;

    @Field()
    hasNextPage: boolean;
  }

  return PaginatedType as Type<IPaginatedType<T>>;
}
