export type IntentType =
  | 'sales'
  | 'stores'
  | 'rewards'
  | 'staff'
  | 'inventory'
  | 'retention'
  | 'billing'
  | 'security'
  | 'analytics'
  | 'general';

export interface ExtractedEntities {
  stores: string[];
  metrics: string[];
  timeframe: string;
  query: string;
}

export interface RouteResult {
  intent: IntentType;
  entities: ExtractedEntities;
}

export function routePrompt(prompt: string): RouteResult {
  const q = prompt.toLowerCase().trim();

  const storesFound: string[] = [];
  if (q.includes('indiranagar')) storesFound.push('Indiranagar Flagship');
  if (q.includes('koramangala')) storesFound.push('Koramangala 80ft');
  if (q.includes('jayanagar')) storesFound.push('Jayanagar 4th Block');
  if (q.includes('whitefield')) storesFound.push('Whitefield Main');

  const metricsFound: string[] = [];
  if (q.includes('revenue') || q.includes('sales') || q.includes('earning')) metricsFound.push('Revenue');
  if (q.includes('visitor') || q.includes('footfall') || q.includes('traffic')) metricsFound.push('Footfall');
  if (q.includes('conversion') || q.includes('rate')) metricsFound.push('Conversion');
  if (q.includes('reward') || q.includes('lyt') || q.includes('points')) metricsFound.push('LYTs');

  let timeframe = 'today';
  if (q.includes('month') || q.includes('30 day')) timeframe = '30d';
  if (q.includes('quarter') || q.includes('90 day')) timeframe = '90d';
  if (q.includes('week') || q.includes('7 day')) timeframe = '7d';

  let intent: IntentType = 'general';

  if (
    q.includes('staff') ||
    q.includes('team') ||
    q.includes('attendance') ||
    q.includes('roster') ||
    q.includes('employee') ||
    q.includes('sneha')
  ) {
    intent = 'staff';
  } else if (
    q.includes('reward') ||
    q.includes('lyt') ||
    q.includes('redeem') ||
    q.includes('points') ||
    q.includes('campaign')
  ) {
    intent = 'rewards';
  } else if (
    q.includes('compare') ||
    q.includes('store') ||
    q.includes('outlet') ||
    q.includes('location') ||
    storesFound.length > 0
  ) {
    intent = 'stores';
  } else if (
    q.includes('inventory') ||
    q.includes('stock') ||
    q.includes('product') ||
    q.includes('reorder') ||
    q.includes('item')
  ) {
    intent = 'inventory';
  } else if (
    q.includes('customer') ||
    q.includes('retention') ||
    q.includes('churn') ||
    q.includes('repeat') ||
    q.includes('cohort')
  ) {
    intent = 'retention';
  } else if (
    q.includes('billing') ||
    q.includes('invoice') ||
    q.includes('subscription') ||
    q.includes('payment') ||
    q.includes('plan')
  ) {
    intent = 'billing';
  } else if (
    q.includes('security') ||
    q.includes('auth') ||
    q.includes('login') ||
    q.includes('password') ||
    q.includes('2fa')
  ) {
    intent = 'security';
  } else if (
    q.includes('sales') ||
    q.includes('revenue') ||
    q.includes('today') ||
    q.includes('report') ||
    q.includes('summary') ||
    q.includes('earnings')
  ) {
    intent = 'sales';
  } else if (
    q.includes('analytics') ||
    q.includes('trend') ||
    q.includes('growth') ||
    q.includes('performance')
  ) {
    intent = 'analytics';
  }

  return {
    intent,
    entities: {
      stores: storesFound,
      metrics: metricsFound,
      timeframe,
      query: prompt,
    },
  };
}
