/**
 * Type definitions for API utilities
 */

import { NextRequest } from "next/server";

/**
 * Validation result interface
 */
export interface ValidationResult {
  valid: boolean;
  error?: string;
}

/**
 * Pagination parameters interface
 */
export interface PaginationParams {
  page: number;
  size: number;
  orderField: string[];
  orderType: string[];
  query: Record<string, any>;
}

/**
 * Pagination response interface
 */
export interface PaginationResponse {
  page: number;
  size: number;
  totalCount: number;
  totalItems: number;
  totalPages: number;
  pageCount: number;
  orderField: string[];
  orderType: string[];
  query: Record<string, any>;
  next: boolean;
  back: boolean;
}

export const defaultPaginationResponse: PaginationResponse = {
  page: 1,
  size: 10,
  totalCount: 0,
  totalItems: 0,
  totalPages: 0,
  pageCount: 0,
  orderField: ["uu_id"],
  orderType: ["asc"],
  query: {},
  next: false,
  back: false,
};

/**
 * API response interface
 */
export interface ApiResponse<T> {
  success: boolean;
  data?: T;
  error?: string;
}

/**
 * Paginated API response interface
 */
export interface PaginatedApiResponse<T> {
  data: T[];
  pagination: PaginationResponse;
}

export const collectPaginationFromApiResponse = (
  response: PaginatedApiResponse<any>
): PaginationResponse => {
  return {
    page: response.pagination?.page || 1,
    size: response.pagination?.size || 10,
    totalCount: response.pagination?.totalCount || 0,
    totalItems: response.pagination?.totalItems || 0,
    totalPages: response.pagination?.totalPages || 0,
    pageCount: response.pagination?.pageCount || 0,
    orderField: response.pagination?.orderField || ["uu_id"],
    orderType: response.pagination?.orderType || ["asc"],
    query: response.pagination?.query || {},
    next: response.pagination?.next || false,
    back: response.pagination?.back || false,
  };
};

/**
 * API handler function types
 */
export type ApiHandlerWithRequest = (request: NextRequest, body: any) => Promise<Response>;
export type ApiHandlerBodyOnly = (body: any) => Promise<Response>;
export type ApiHandler = ApiHandlerWithRequest | ApiHandlerBodyOnly;

/**
 * List function type
 */
export type ListFunction = (
  params: PaginationParams
) => Promise<PaginatedApiResponse<any>>;

/**
 * Create function type
 */
export type CreateFunction = (data: any) => Promise<any>;

/**
 * Update function type
 */
export type UpdateFunction = (id: any, data: any) => Promise<any>;

/**
 * Delete function type
 */
export type DeleteFunction = (id: any) => Promise<any>;
