import type {ExtractedEntities} from '../promptRouter';
import type {ReportPart} from '@/features/loyaly-ai/types/chat';

export function buildStoresTemplate(entities: ExtractedEntities): ReportPart {
  const isSpecificComparison = entities.stores.length >= 2;
  const storeNames = isSpecificComparison ? entities.stores.join(' vs ') : 'Multi-Store Network';

  return {
    type: 'report',
    title: `Store Comparison Analysis: ${storeNames}`,
    summary: `Side-by-side performance evaluation for **${entities.query}**. Indiranagar Flagship continues to lead the network in conversion rate (23.8%), while Whitefield Main presents the highest potential for conversion recovery.`,
    kpis: [
      {id: 'stk1', title: 'Top Store Revenue', value: '₹9.1L', trend: 'Indiranagar Flagship', iconName: 'stores'},
      {id: 'stk2', title: 'Avg Network Conversion', value: '21.1%', trend: '+1.4% vs last mo', iconName: 'conversion'},
      {id: 'stk3', title: 'Active Outlets', value: '4 Tracked', iconName: 'stores'},
    ],
    tables: [
      {
        title: 'Network Outlets Comparison Breakdown',
        columns: [
          {key: 'store', header: 'Store Location'},
          {key: 'visitors', header: 'Visitors', align: 'end'},
          {key: 'revenue', header: 'Revenue', align: 'end'},
          {key: 'conversion', header: 'Conversion', align: 'end'},
          {key: 'status', header: 'Status', align: 'center'},
        ],
        rows: [
          {store: 'Indiranagar Flagship', visitors: '12,400', revenue: '₹9.1L', conversion: '23.8%', status: 'active'},
          {store: 'Koramangala 80ft', visitors: '7,800', revenue: '₹5.2L', conversion: '21.4%', status: 'active'},
          {store: 'Jayanagar 4th Block', visitors: '6,100', revenue: '₹4.0L', conversion: '20.9%', status: 'active'},
          {store: 'Whitefield Main', visitors: '4,200', revenue: '₹3.3L', conversion: '18.2%', status: 'warning'},
        ],
      },
    ],
    insights: [
      {
        id: 'sti1',
        type: 'best_performer',
        title: 'Indiranagar Benchmark Conversion',
        explanation: 'Indiranagar conversion rate exceeds network average by +2.7 percentage points.',
        confidence: 98,
      },
      {
        id: 'sti2',
        type: 'risk',
        title: 'Whitefield Conversion Underperformance',
        explanation: 'Whitefield is 5.6 points behind flagship despite high visitor traffic per staff seat.',
        confidence: 89,
        recommendedAction: 'Conduct staff training on checkout upsell workflows.',
      },
    ],
    actions: [
      {id: 'sta1', label: 'View Stores Directory', actionType: 'compare_stores', iconName: 'stores'},
      {id: 'sta2', label: 'Download Store Data CSV', actionType: 'export_csv', iconName: 'download'},
    ],
  };
}
