import { Types } from "mongoose";
import { z } from "zod";

// TODO improve this type to more representative of a db object
export type TData = object;

export const paginatedRequestSchema = z.object({
  /**
   * The page number to retrieve
   */
  pageIndex: z.coerce.number().int().optional().default(0),
  /**
   * The number of items retrieve
   */
  pageSize: z.coerce.number().int().optional().default(10),
  /**
   * Search query
   */
  search: z.string().optional(),
  /**
   * String array of filters
   */
  filters: z.string().array().optional(),
});

export type PaginatedRequest = z.infer<typeof paginatedRequestSchema>;

export interface PaginatedResponse<T> {
  data: T[];
  nextPage: number | undefined;
  pageCount: number;
}

export const pageLimitOptions = [10, 25, 50, 100];

/**
 * A field which can be displayed in the data table as a result of a database query or transformer
 */
export type DataTableField =
  | string
  | number
  | Date
  | Types.ObjectId
  | JSX.Element
  | null
  | undefined
  | boolean
  | any
  | any[];

/**
 * A data table record which is either a database record as TData or a DataTableField
 */
export type DataTableRecord<T extends TData> =
  | T
  | Record<string, DataTableField>;

/**
 * A data transformer which can be used to transform the data to be displayed in the table.
 */
export type DataTableTransformer<T extends TData> = (
  data: T[]
) => DataTableRecord<T>[];
