/**
 * Global Request Navigation Utility
 * 
 * Centralized navigation logic for request-related routes.
 * This utility decides where to navigate when clicking on request cards
 * from anywhere in the application.
 * 
 * Features:
 * - Single point of navigation logic
 * - Handles draft vs active requests
 * - Supports different flow types (CUSTOM, DEALER_CLAIM)
 * - Type-safe navigation
 */

import { NavigateFunction } from 'react-router-dom';
import { getRequestDetailRoute, RequestFlowType } from './requestTypeUtils';

export interface RequestNavigationOptions {
  requestId: string;
  requestTitle?: string;
  status?: string;
  request?: any; // Full request object if available
  navigate: NavigateFunction;
}

/**
 * Navigate to the appropriate request detail page based on request type
 * 
 * This is the single point of navigation for all request cards.
 * It handles:
 * - Draft requests (navigate to edit)
 * - Different flow types (CUSTOM, DEALER_CLAIM)
 * - Status-based routing
 */
export function navigateToRequest(options: RequestNavigationOptions): void {
  const { requestId, status, request, navigate } = options;

  // Check if request is a draft - if so, route to edit form instead of detail view
  const isDraft = status?.toLowerCase() === 'draft' || status === 'DRAFT';
  if (isDraft) {
    navigate(`/edit-request/${requestId}`);
    return;
  }

  // Determine the appropriate route based on request type
  const route = getRequestDetailRoute(requestId, request);
  navigate(route);
}

/**
 * Navigate to create a new request based on flow type
 */
export function navigateToCreateRequest(
  navigate: NavigateFunction,
  flowType: RequestFlowType = 'CUSTOM'
): void {
  const route = flowType === 'DEALER_CLAIM' 
    ? '/claim-management' 
    : '/new-request';
  navigate(route);
}

/**
 * Create a navigation handler function for request cards
 * This can be used directly in onClick handlers
 */
export function createRequestNavigationHandler(
  navigate: NavigateFunction
) {
  return (requestId: string, requestTitle?: string, status?: string, request?: any) => {
    navigateToRequest({
      requestId,
      requestTitle,
      status,
      request,
      navigate,
    });
  };
}
