"use client"; import { useEffect, useState } from "react"; import axios from "axios"; import { Radar, RadarChart, PolarGrid, PolarAngleAxis, PolarRadiusAxis, BarChart, Bar, XAxis, YAxis, Tooltip, CartesianGrid, ResponsiveContainer, LineChart, Line, PieChart, Pie, Cell, Legend } from 'recharts'; import { InfoTooltip } from "@/components/ui/tooltip"; const API_URL = "/api"; const COLORS = ['#10b981', '#f59e0b', '#ef4444', '#3b82f6']; export function AnalyticsSection({ definitions }: { definitions: any }) { const [globalData, setGlobalData] = useState(null); const [complexity, setComplexity] = useState([]); useEffect(() => { // Fetch new global analytics axios.get(`${API_URL}/analytics/global`).then(res => setGlobalData(res.data)).catch(err => console.error(err)); // Keep existing complexity data axios.get(`${API_URL}/analytics/finish-complexity`).then(res => setComplexity(res.data)); }, []); if (!globalData) return
Loading Global Insights...
; const { kpis, distributions, trends, global_waterfall, global_blame } = globalData; // Prepare blame data for Pie chart const blameData = global_blame ? [ { name: 'Policy (norms)', value: global_blame.policy_pct, color: '#f59e0b' }, { name: 'Execution (planner)', value: global_blame.execution_pct, color: '#3b82f6' }, { name: 'Process (manufacturing)', value: global_blame.process_pct, color: '#ef4444' } ] : []; return (
{/* 1. Global KPI Cards */}
95 ? "text-emerald-400" : "text-amber-400"} tooltip="Weighted manufacturing efficiency. Formula: (Σ Pack Fresh Output ÷ Σ Greige Issued) × 100. Higher = less process loss." />
{/* 2. Charts Grid */}
{/* Finish Performance */} {/* Route Performance */} {/* NEW: Yield by Shade */} {distributions.shade && distributions.shade.length > 0 && ( )} {/* Monthly Trend */} {/* Complexity Bar Chart (Fixed from Radar) */} [`${Number(value).toFixed(2)}%`, 'Avg Deviation']} /> {/* NEW: Global Blame/Friction Breakdown */} {global_blame && ( {blameData.map((entry, index) => ( ))} [`${Number(value).toFixed(1)}%`, 'Share of Shortfall']} /> )}
{/* 3. NEW: Top Customers & Segment Analysis */} {(distributions.customer?.length > 0 || distributions.segment?.length > 0) && (
{/* Top Customers Table */} {distributions.customer && distributions.customer.length > 0 && (

Top Customers

By Order Quantity (Meters)

{distributions.customer.map((c: any, i: number) => ( ))}
Customer Order Qty Efficiency %
{c.customer} {(c.volume / 1000).toFixed(1)}K 95 ? 'text-emerald-400' : 'text-amber-400'}`}>{c.yield}%
)} {/* Segment Distribution */} {distributions.segment && distributions.segment.length > 0 && ( `${(v / 1000).toFixed(0)}K`} /> [`${(value / 1000).toFixed(1)}K meters`, 'Order Qty']} /> )}
) } {/* 4. NEW: Global Waterfall (Opportunity Loss) */} { global_waterfall && (

Global Opportunity Waterfall

Order Quantity → Delivery Flow (Meters)

`${(v / 1000000).toFixed(1)}M`} /> { const isPositive = props.payload.value >= 0; return [`${isPositive ? '+' : ''}${(Number(value) / 1000000).toFixed(2)}M`, props.payload.desc || props.payload.label]; }} /> {global_waterfall.map((entry: any, index: number) => { const bgColor = entry.type === 'base' ? '#3b82f6' : entry.type === 'final' ? '#10b981' : entry.value >= 0 ? '#f59e0b' : '#ef4444'; return ; })}
) }
) } function KPICard({ label, value, sub, color = "text-white", tooltip }: { label: string, value: string | number, sub: string, color?: string, tooltip?: string }) { return (

{label} {tooltip && }

{value}
{sub}
) } function ChartCard({ title, subtitle, description, children }: { title: string, subtitle: string, description?: string, children: React.ReactNode }) { return (

{title} {description && }

{subtitle}

{children}
) }