import { GoogleGenAI } from "@google/genai"; import { AggregatedData } from "./types"; // Declare process to avoid TypeScript errors without causing aggressive bundler shims declare const process: any; const SYSTEM_INSTRUCTION = ` You are a senior data analyst assistant for a retail dashboard called "Craze Analytix". You have access to a detailed report of the currently filtered sales data. The data includes Sell Out (Revenue in €), Units Sold, Product Lines, Customers/Markets, and Seasonality trends. Your goal is to answer user questions specific to the provided data. - If asked about "Trends" or "Seasonality", look at the Monthly Seasonality section. - If asked about "Growth" or "Decline", look at the Top/Bottom Movers sections. - If asked about specific Product Lines, look at the Product Line Breakdown. - Always format numbers clearly (e.g., "€1.2M", "€5,200", "15k units"). - When comparing years, calculate the percentage difference if not explicitly provided. - Keep answers professional, concise, and business-focused. `; // Helper to get API key safely const getApiKey = (): string | undefined => { try { return process.env.API_KEY; } catch (e) { return undefined; } }; const formatCurrency = (val: number) => `€${val.toLocaleString('de-DE', {minimumFractionDigits: 0, maximumFractionDigits: 0})}`; const formatUnits = (val: number) => `${val.toLocaleString('de-DE')} units`; export const queryGemini = async ( question: string, context: AggregatedData, filteredRecordCount: number ): Promise => { const apiKey = getApiKey(); if (!apiKey) { return "API Key is missing. Please configure your environment variables (API_KEY) or check your .env file."; } try { const ai = new GoogleGenAI({ apiKey }); // --- CONTEXT GENERATION --- // We construct a structured report mirroring the dashboard charts // 1. Totals by Year (KPI Cards) const yearlySummary = Object.entries(context.totalsByYear) .sort((a, b) => parseInt(b[0]) - parseInt(a[0])) // Descending years .map(([year, data]) => ` - ${year}: ${formatCurrency(data.sellOut)} | ${formatUnits(data.units)}`) .join('\n'); // 2. Seasonality (Line Chart Data) // We simplify this to a CSV-like list for the AI to parse trends const seasonalitySummary = context.seasonality.map(p => { // Extract values for each year in the point const yearValues = context.availableYears.map(y => `${y}: ${formatCurrency(p[y] as number || 0)}`).join(', '); return ` - ${p.name}: [${yearValues}]`; }).join('\n'); // 3. Top Movers (Growth Table) - Limit to Top 10 const growthSummary = context.topMovers.slice(0, 10).map(m => ` - ${m.line}: +€${m.sellOutGrowthValue.toLocaleString('de-DE')} (${m.sellOutGrowthPercentage.toFixed(1)}%)` ).join('\n'); // 4. Declining Movers (Decline Table) - Limit to Top 10 const declineSummary = context.bottomMovers.slice(0, 10).map(m => ` - ${m.line}: -€${Math.abs(m.sellOutGrowthValue).toLocaleString('de-DE')} (${m.sellOutGrowthPercentage.toFixed(1)}%)` ).join('\n'); // 5. Product Lines Overview (Bar Charts) - Limit to Top 50 to save tokens but give depth const topLinesSummary = context.byLine.slice(0, 50).map((l, i) => ` ${i+1}. ${l.name}: ${formatCurrency(l.value)} | ${formatUnits(l.units)}` ).join('\n'); // 6. Customer Distribution (Customer Chart) const customerSummary = context.byCustomer.map(c => ` - ${c.name}: ${formatCurrency(c.value)}` ).join('\n'); const fullReport = ` REPORT CONTEXT: ---------------- GLOBAL TOTALS: Total Sell Out: ${formatCurrency(context.totalSellOut)} Total Units: ${formatUnits(context.totalUnits)} Records Analyzed: ${filteredRecordCount} Years Available: ${context.availableYears.join(', ')} YEARLY BREAKDOWN: ${yearlySummary} MONTHLY SEASONALITY (Revenue Trends): ${seasonalitySummary} FASTEST GROWING LINES (Year-over-Year): ${growthSummary} DECLINING LINES (Year-over-Year): ${declineSummary} TOP PRODUCT LINES (Revenue & Units): ${topLinesSummary} PERFORMANCE BY CUSTOMER: ${customerSummary} `; const response = await ai.models.generateContent({ model: 'gemini-2.5-flash', contents: `Context Data:\n${fullReport}\n\nUser Question: ${question}`, config: { systemInstruction: SYSTEM_INSTRUCTION, } }); return response.text || "I couldn't generate a response based on the data provided."; } catch (error: any) { console.error("Gemini API Error:", error); if (error.message && error.message.includes("Not implemented on this platform")) { return "System Error: The AI SDK detected a platform mismatch."; } return `Error: ${error.message || "An unexpected error occurred while analyzing the data."}`; } };