"use client"; import { create } from "zustand"; interface AiInsight { codein: string; insight: string; status: "idle" | "loading" | "done" | "error"; } interface AiCopilotState { insights: Record; analyzeProduct: (payload: { codein: string; libelle1: string; totalCa: number; tauxMarge: number; totalQuantite: number; sales12m: Record; codeGamme: string | null; }) => Promise; } export const useAiCopilotStore = create((set) => ({ insights: {}, analyzeProduct: async (payload) => { const { codein } = payload; // Mark as loading set((s) => ({ insights: { ...s.insights, [codein]: { codein, insight: "", status: "loading" } }, })); try { const res = await fetch("/api/ai/analyze", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), }); if (!res.ok) throw new Error(`HTTP ${res.status}`); const data = await res.json(); set((s) => ({ insights: { ...s.insights, [codein]: { codein, insight: data.insight, status: "done" }, }, })); } catch { set((s) => ({ insights: { ...s.insights, [codein]: { codein, insight: "Erreur lors de l'analyse.", status: "error" }, }, })); } }, }));