/**
 * Standard API Response Type
 * All responses return HTTP 200 with this structure
 */
export type ApiResponse<T = any> = {
  errors: any[];
  stack?: string;
  message: string;
  success: boolean;
  status: number;
  data: T;
};

/**
 * Paginated response wrapper
 */
export interface PaginatedData<T> {
  items: T[];
  meta: PaginationMeta;
}

export interface PaginationMeta {
  total: number;
  page: number;
  limit: number;
  totalPages: number;
  hasNextPage: boolean;
  hasPreviousPage: boolean;
}

/**
 * Create a successful API response
 */
export function createSuccessResponse<T>(
  data: T,
  message = 'Success',
  status = 200,
): ApiResponse<T> {
  return {
    success: true,
    status,
    message,
    data,
    errors: [],
  };
}

/**
 * Create an error API response
 */
export function createErrorResponse(
  message: string,
  status = 400,
  errors: any[] = [],
  stack?: string,
): ApiResponse<null> {
  return {
    success: false,
    status,
    message,
    data: null,
    errors,
    stack,
  };
}

/**
 * Create a paginated API response
 */
export function createPaginatedResponse<T>(
  items: T[],
  total: number,
  page: number,
  limit: number,
  message = 'Success',
): ApiResponse<PaginatedData<T>> {
  const totalPages = Math.ceil(total / limit);

  return {
    success: true,
    status: 200,
    message,
    data: {
      items,
      meta: {
        total,
        page,
        limit,
        totalPages,
        hasNextPage: page < totalPages,
        hasPreviousPage: page > 1,
      },
    },
    errors: [],
  };
}
