import { Request } from "express";

// API 응답 타입
export interface ApiResponse<T = any> {
  success: boolean;
  data?: T;
  message?: string;
  total?: number;
  error?: {
    code: string;
    details?: any;
  };
  pagination?: {
    page: number;
    limit: number;
    total: number;
    totalPages: number;
  };
}

// 페이징 파라미터 타입
export interface PaginationParams {
  page?: number;
  limit?: number;
  sortBy?: string;
  sortOrder?: "asc" | "desc";
}

// 검색 파라미터 타입
export interface SearchParams extends PaginationParams {
  search?: string;
  filters?: Record<string, any>;
}

// JWT 페이로드 타입
export interface JWTPayload {
  userId: string;
  userName?: string;
  email?: string;
  deptCode?: string;
  permissions?: string[];
  iat: number;
  exp: number;
}

// 인증된 요청 타입
export interface AuthenticatedRequest extends Request {
  user?: JWTPayload;
}

// 파일 업로드 타입
export interface FileUpload {
  fieldname: string;
  originalname: string;
  encoding: string;
  mimetype: string;
  size: number;
  destination: string;
  filename: string;
  path: string;
}

// 성공 응답 헬퍼 함수
export const successResponse = <T>(
  data: T,
  message?: string
): ApiResponse<T> => ({
  success: true,
  data,
  message,
});

// 에러 응답 헬퍼 함수
export const errorResponse = (
  message: string,
  code?: string,
  details?: any
): ApiResponse<never> => ({
  success: false,
  error: {
    code: code || "UNKNOWN_ERROR",
    details,
  },
  message,
});

// 페이징 응답 헬퍼 함수
export const paginatedResponse = <T>(
  data: T[],
  page: number,
  limit: number,
  total: number,
  message?: string
): ApiResponse<T[]> => ({
  success: true,
  data,
  message,
  pagination: {
    page,
    limit,
    total,
    totalPages: Math.ceil(total / limit),
  },
});
