/**
 * Service Type to Page Routing Utility
 * Feature: service-appointment-forms, Property 7: Service Type Routing
 * Validates: Requirements 17.1
 * 
 * For any service selection, the system SHALL navigate to the correct 
 * form page based on the service type mapping.
 */

/**
 * Service type to page mapping configuration
 * Maps service type identifiers to their corresponding form pages and categories
 */
export const SERVICE_TYPE_MAP = {
  // Airfare (existing)
  airfare: {
    category: 'travel',
    page: 'airfare-info-entry-page',
    path: '/pages/appointment/airfare-info-entry-page'
  },
  
  // Accommodation
  hotel: {
    category: 'accommodation',
    page: 'hotel-reservation-page',
    path: '/pages/appointment/hotel-reservation-page'
  },
  
  // Travel
  vip_lounge: {
    category: 'travel',
    page: 'vip-lounge-page',
    path: '/pages/appointment/vip-lounge-page'
  },
  airport_transfer: {
    category: 'travel',
    page: 'airport-transfer-page',
    path: '/pages/appointment/airport-transfer-page'
  },
  rail_ticket: {
    category: 'travel',
    page: 'rail-ticket-page',
    path: '/pages/appointment/rail-ticket-page'
  },
  
  // Other
  unaccompanied_minor: {
    category: 'other',
    page: 'unaccompanied-minor-page',
    path: '/pages/appointment/unaccompanied-minor-page'
  },
  medical_consultation: {
    category: 'other',
    page: 'medical-consultation-page',
    path: '/pages/appointment/medical-consultation-page'
  },
  special_needs: {
    category: 'other',
    page: 'special-needs-page',
    path: '/pages/appointment/special-needs-page'
  },
  pet_transportation: {
    category: 'other',
    page: 'pet-transportation-page',
    path: '/pages/appointment/pet-transportation-page'
  },
  air_logistics: {
    category: 'other',
    page: 'air-logistics-page',
    path: '/pages/appointment/air-logistics-page'
  },
  sea_freight: {
    category: 'other',
    page: 'sea-freight-page',
    path: '/pages/appointment/sea-freight-page'
  },
  
  // Guide/Translation
  guide_translation: {
    category: 'guide_translation',
    page: 'guide-translation-page',
    path: '/pages/appointment/guide-translation-page'
  },
  travel_planning: {
    category: 'guide_consulting',
    page: 'travel-planning-page',
    path: '/pages/appointment/travel-planning-page'
  },
  
  // Consulting
  visa_consultation: {
    category: 'consulting',
    page: 'visa-consultation-page',
    path: '/pages/appointment/visa-consultation-page'
  },
  exhibition_service: {
    category: 'consulting',
    page: 'exhibition-service-page',
    path: '/pages/appointment/exhibition-service-page'
  },
  insurance_consultation: {
    category: 'consulting',
    page: 'insurance-consultation-page',
    path: '/pages/appointment/insurance-consultation-page'
  }
};

/**
 * Gets the form page path for a given service type
 * Feature: service-appointment-forms, Property 7: Service Type Routing
 * Validates: Requirements 17.1
 * 
 * @param {string} serviceType - The service type identifier
 * @returns {string|null} The page path or null if service type is unknown
 */
export function getFormPageByServiceType(serviceType) {
  if (!serviceType || typeof serviceType !== 'string') {
    return null;
  }
  
  const config = SERVICE_TYPE_MAP[serviceType];
  return config ? config.path : null;
}

/**
 * Gets the full configuration for a service type
 * 
 * @param {string} serviceType - The service type identifier
 * @returns {object|null} The service configuration or null if unknown
 */
export function getServiceConfig(serviceType) {
  if (!serviceType || typeof serviceType !== 'string') {
    return null;
  }
  
  return SERVICE_TYPE_MAP[serviceType] || null;
}

/**
 * Gets all service types for a given category
 * 
 * @param {string} category - The category to filter by
 * @returns {string[]} Array of service type identifiers in that category
 */
export function getServiceTypesByCategory(category) {
  if (!category || typeof category !== 'string') {
    return [];
  }
  
  return Object.entries(SERVICE_TYPE_MAP)
    .filter(([_, config]) => config.category === category)
    .map(([serviceType, _]) => serviceType);
}

/**
 * Gets all available service types
 * 
 * @returns {string[]} Array of all service type identifiers
 */
export function getAllServiceTypes() {
  return Object.keys(SERVICE_TYPE_MAP);
}

/**
 * Checks if a service type is valid/known
 * 
 * @param {string} serviceType - The service type to check
 * @returns {boolean} True if the service type is known
 */
export function isValidServiceType(serviceType) {
  return serviceType && typeof serviceType === 'string' && SERVICE_TYPE_MAP.hasOwnProperty(serviceType);
}

/**
 * Navigates to the appropriate form page for a service type
 * 
 * @param {string} serviceType - The service type identifier
 * @param {object} params - Additional URL parameters (id, title, etc.)
 * @returns {boolean} True if navigation was initiated, false if service type is unknown
 */
export function navigateToServiceForm(serviceType, params = {}) {
  const pagePath = getFormPageByServiceType(serviceType);
  
  if (!pagePath) {
    console.warn(`Unknown service type: ${serviceType}`);
    return false;
  }
  
  // Build query string from params
  const queryParts = [];
  for (const [key, value] of Object.entries(params)) {
    if (value !== undefined && value !== null) {
      queryParts.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
    }
  }
  
  const queryString = queryParts.length > 0 ? `?${queryParts.join('&')}` : '';
  const fullPath = `${pagePath}${queryString}`;
  
  uni.navigateTo({
    url: fullPath
  });
  
  return true;
}

export default {
  SERVICE_TYPE_MAP,
  getFormPageByServiceType,
  getServiceConfig,
  getServiceTypesByCategory,
  getAllServiceTypes,
  isValidServiceType,
  navigateToServiceForm
};
