/**
 * Request Type Detection and Utilities
 * 
 * Centralized utility for identifying request types and determining
 * which flow/components to use for each request type.
 * 
 * Supported Types:
 * - CUSTOM: Standard custom requests
 * - DEALER_CLAIM: Dealer claim management requests
 */

export type RequestFlowType = 'CUSTOM' | 'DEALER_CLAIM';

/**
 * Check if a request is a Dealer Claim request
 * Supports both old and new backend formats
 */
export function isDealerClaimRequest(request: any): boolean {
  if (!request) return false;

  // New format: Check workflowType
  if (request.workflowType === 'CLAIM_MANAGEMENT' || request.workflowType === 'DEALER_CLAIM') {
    return true;
  }

  // Old format: Check templateType (for backward compatibility)
  if (request.templateType === 'claim-management' || 
      request.template === 'claim-management' ||
      request.templateType === 'dealer-claim') {
    return true;
  }

  // Check template name/code
  if (request.templateName === 'Claim Management' || 
      request.templateCode === 'CLAIM_MANAGEMENT' ||
      request.templateCode === 'DEALER_CLAIM') {
    return true;
  }

  return false;
}

/**
 * Check if a request is a Custom request
 */
export function isCustomRequest(request: any): boolean {
  if (!request) return false;

  // If it's explicitly marked as custom
  if (request.workflowType === 'CUSTOM' || 
      request.workflowType === 'NON_TEMPLATIZED' ||
      request.templateType === 'custom' ||
      request.template === 'custom') {
    return true;
  }

  // If it's not a dealer claim, assume it's custom
  // This handles legacy requests that don't have explicit type
  if (!isDealerClaimRequest(request)) {
    return true;
  }

  return false;
}

/**
 * Get the flow type for a request
 * Returns the appropriate RequestFlowType based on request properties
 */
export function getRequestFlowType(request: any): RequestFlowType {
  if (isDealerClaimRequest(request)) {
    return 'DEALER_CLAIM';
  }
  return 'CUSTOM';
}

/**
 * Get the route path for a request detail page based on flow type
 */
export function getRequestDetailRoute(requestId: string, _request?: any): string {
  // For now, all requests use the same route
  // In the future, you can customize routes per flow type:
  // if (flowType === 'DEALER_CLAIM') {
  //   return `/dealer-claim/${requestId}`;
  // }
  // return `/custom-request/${requestId}`;
  
  return `/request/${requestId}`;
}

/**
 * Get the route path for creating a new request based on flow type
 */
export function getCreateRequestRoute(flowType: RequestFlowType): string {
  switch (flowType) {
    case 'DEALER_CLAIM':
      return '/claim-management';
    case 'CUSTOM':
    default:
      return '/new-request';
  }
}

/**
 * Check if request needs flow-specific UI components
 */
export function shouldUseFlowSpecificUI(request: any, flowType: RequestFlowType): boolean {
  return getRequestFlowType(request) === flowType;
}
