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

export function buildSalesTemplate(entities: ExtractedEntities): ReportPart {
  const storeContext = entities.stores.length > 0 ? entities.stores.join(', ') : 'All Active Outlets';

  return {
    type: 'report',
    title: `Sales & Revenue Performance — ${storeContext}`,
    summary: `Analysis generated for **"${entities.query}"**. Total revenue across ${storeContext} reached **₹3,40,000** today (+2.8% vs yesterday). Conversion rate stands at **21.5%** across 1,284 store visits.`,
    kpis: [
      {
        id: 'k1',
        title: 'Total Revenue',
        value: '₹3,40,000',
        trend: '+2.8%',
        trendDirection: 'up',
        iconName: 'revenue',
        sparkline: [2.8, 3.1, 2.9, 3.2, 3.4],
      },
      {
        id: 'k2',
        title: 'Visitor Footfall',
        value: '1,284',
        trend: '+4.2%',
        trendDirection: 'up',
        iconName: 'visitors',
        sparkline: [1150, 1200, 1180, 1240, 1284],
      },
      {
        id: 'k3',
        title: 'Completed Purchases',
        value: '276',
        trend: '-1.1%',
        trendDirection: 'down',
        iconName: 'purchases',
        sparkline: [290, 285, 280, 278, 276],
      },
      {
        id: 'k4',
        title: 'LYTs Rewarded',
        value: '34,100',
        trend: '+6.0%',
        trendDirection: 'up',
        iconName: 'lyt',
        sparkline: [28000, 30000, 31500, 32000, 34100],
      },
    ],
    charts: [
      {
        title: 'Hourly Revenue & Footfall Breakdown',
        subtitle: 'Real-time telemetry updated every 15 minutes',
        chartType: 'area',
        xKey: 'time',
        data: [
          {time: '10:00', Revenue: 25000, Footfall: 120},
          {time: '12:00', Revenue: 62000, Footfall: 240},
          {time: '14:00', Revenue: 85000, Footfall: 310},
          {time: '16:00', Revenue: 78000, Footfall: 280},
          {time: '18:00', Revenue: 90000, Footfall: 334},
        ],
        series: [
          {key: 'Revenue', label: 'Revenue (₹)'},
          {key: 'Footfall', label: 'Visitors'},
        ],
      },
    ],
    insights: [
      {
        id: 'si1',
        type: 'growth',
        title: 'Peak Evening Surge Detected',
        explanation: 'Indiranagar Flagship experienced a 38% revenue spike between 17:30 and 19:30 IST.',
        confidence: 96,
        recommendedAction: 'Ensure 2 extra staff members are assigned to express checkout lanes after 17:00.',
      },
      {
        id: 'si2',
        type: 'opportunity',
        title: 'Basket Size Upsell Opportunity',
        explanation: 'Average order value in Koramangala is ₹1,230 vs ₹1,680 in Indiranagar.',
        confidence: 91,
        recommendedAction: 'Trigger a +50 LYT bonus for orders exceeding ₹1,500.',
      },
    ],
    actions: [
      {id: 'sa1', label: 'Open Main Dashboard', actionType: 'open_dashboard', iconName: 'dashboard'},
      {id: 'sa2', label: 'Compare Outlet Trends', actionType: 'compare_stores', iconName: 'stores'},
      {id: 'sa3', label: 'Export Sales CSV', actionType: 'export_csv', iconName: 'download'},
    ],
  };
}
