diff --git a/em2rp/.gitignore b/em2rp/.gitignore index 4fb460d..df804c4 100644 --- a/em2rp/.gitignore +++ b/em2rp/.gitignore @@ -47,3 +47,4 @@ lib/config/env.dev.dart functions/.env .env env.dart +functions/.env.local diff --git a/em2rp/functions/.env b/em2rp/functions/.env index 5af40c5..cd93c6f 100644 --- a/em2rp/functions/.env +++ b/em2rp/functions/.env @@ -7,3 +7,4 @@ SMTP_PASS="aL8@Rx8xqFrNij$a" # URL de l'application APP_URL="https://app.em2events.fr" +GEMINI_API_KEY="AIzaSyBdBdjFLma2pLenmFBlqZHArS4GVF-mclo" diff --git a/em2rp/functions/aiEquipmentProposal.js b/em2rp/functions/aiEquipmentProposal.js new file mode 100644 index 0000000..b6c068c --- /dev/null +++ b/em2rp/functions/aiEquipmentProposal.js @@ -0,0 +1,1818 @@ +/** + * Cloud Function : Assistant IA Logisticien + * Utilise Gemini avec function calling côté serveur. + * Les tools accèdent directement à Firestore via Admin SDK. + * L'authentification Firebase est requise (pas de clé API côté client). + */ + +const { GoogleGenerativeAI } = require('@google/generative-ai'); +const admin = require('firebase-admin'); +const logger = require('firebase-functions/logger'); + +const GEMINI_MODEL = 'gemini-3.1-flash-lite'; //Ne pas changer de version, celle ci existe +const GEMINI_API_KEY = process.env.GEMINI_API_KEY || process.env.GOOGLE_AI_API_KEY || ''; + +// Log de démarrage pour debug +if (GEMINI_API_KEY) { + const maskedKey = GEMINI_API_KEY.substring(0, 8) + '...' + GEMINI_API_KEY.substring(GEMINI_API_KEY.length - 5); + logger.info(`[AI] GEMINI_API_KEY chargée (masquée: ${maskedKey})`); +} else { + logger.warn('[AI] ⚠️ GEMINI_API_KEY non trouvée ! Vérifiez .env.local ou les variables d\'environnement Firebase'); +} +const MAX_TOOL_ITERATIONS = 20; +const PAST_EVENTS_LIMIT = 5; +const SEARCH_RESULTS_LIMIT = 50; // Augmenté pour permettre de trouver plusieurs unités du même modèle +const EVENT_SEARCH_SCAN_LIMIT = 100; +const MAX_TOOL_CALLS_PER_ITERATION = 40; +const AVAILABILITY_EVENTS_SCAN_LIMIT = 500; +const MAX_BATCH_AVAILABILITY_ITEMS = 50; + +// Initialisation lazy de db pour éviter les erreurs si Firebase n'est pas encore initialisé +const getDb = () => admin.firestore(); + +// ============================================================================ +// Déclarations des tools Gemini +// ============================================================================ +const AI_TOOLS = [ + { + functionDeclarations: [ + { + name: 'search_equipment', + description: 'Recherche du materiel par mot-cle dans la base de données.', + parameters: { + type: 'object', + properties: { + query: { + type: 'string', + description: 'Texte de recherche (nom, catégorie, marque, modèle).', + }, + }, + required: ['query'], + }, + }, + { + name: 'check_availability', + description: 'Vérifie si un équipement est disponible pour une période donnée.', + parameters: { + type: 'object', + properties: { + equipmentId: { + type: 'string', + description: 'ID exact de l\'équipement à vérifier.', + }, + startDate: { + type: 'string', + description: 'Date de début ISO-8601. Exemple: 2026-03-20T08:00:00.000Z', + }, + endDate: { + type: 'string', + description: 'Date de fin ISO-8601. Exemple: 2026-03-21T23:00:00.000Z', + }, + }, + required: ['equipmentId', 'startDate', 'endDate'], + }, + }, + { + name: 'check_availability_batch', + description: 'Vérifie la disponibilité d une liste d équipements pour la meme période en un seul appel.', + parameters: { + type: 'object', + properties: { + equipmentIds: { + type: 'array', + items: { type: 'string' }, + description: 'Liste des IDs à vérifier (max 50).', + }, + startDate: { + type: 'string', + description: 'Date de début ISO-8601.', + }, + endDate: { + type: 'string', + description: 'Date de fin ISO-8601.', + }, + }, + required: ['equipmentIds', 'startDate', 'endDate'], + }, + }, + { + name: 'get_past_events', + description: `Retourne les ${PAST_EVENTS_LIMIT} événements passés les plus récents similaires, avec leur liste de matériel, pour s'en inspirer.`, + parameters: { + type: 'object', + properties: { + eventTypeId: { + type: 'string', + description: 'ID du type d\'événement. Optionnel.', + nullable: true, + }, + }, + }, + }, + { + name: 'search_event_reference', + description: 'Recherche un evenement precise par nom (et optionnellement date) pour reutiliser son materiel et ses flight cases.', + parameters: { + type: 'object', + properties: { + query: { + type: 'string', + description: 'Texte du nom ou extrait du nom de l evenement recherche.', + }, + dateHint: { + type: 'string', + description: 'Date cible ISO-8601 ou YYYY-MM-DD si connue.', + nullable: true, + }, + }, + required: ['query'], + }, + }, + { + name: 'search_containers', + description: 'Recherche des flight cases / containers contenant certains equipements. Toujours appeler avant de proposer du materiel individuellement afin de privilegier les flight cases.', + parameters: { + type: 'object', + properties: { + equipmentIds: { + type: 'array', + items: { type: 'string' }, + description: 'Liste des IDs d equipements pour lesquels chercher des flight cases.', + }, + query: { + type: 'string', + description: 'Recherche textuelle optionnelle dans le nom du container.', + nullable: true, + }, + }, + required: ['equipmentIds'], + }, + }, + { + name: 'check_container_availability', + description: 'Vérifie si un container (flight case) et son contenu sont disponibles pour une période donnée.', + parameters: { + type: 'object', + properties: { + containerId: { type: 'string', description: 'ID exact du container à vérifier.' }, + startDate: { type: 'string', description: 'Date de début ISO-8601.' }, + endDate: { type: 'string', description: 'Date de fin ISO-8601.' }, + }, + required: ['containerId', 'startDate', 'endDate'], + }, + }, + { + name: 'check_container_availability_batch', + description: 'Vérifie la disponibilité d une liste de containers pour la même période en un seul appel.', + parameters: { + type: 'object', + properties: { + containerIds: { type: 'array', items: { type: 'string' }, description: 'Liste des IDs de containers (max 50).' }, + startDate: { type: 'string', description: 'Date de début ISO-8601.' }, + endDate: { type: 'string', description: 'Date de fin ISO-8601.' }, + }, + required: ['containerIds', 'startDate', 'endDate'], + }, + }, + { + name: 'list_equipment_by_category', + description: 'Liste le materiel d\'une categorie ou sous-categorie specifique. Utile si search_equipment ne donne rien a cause d\'une faute de frappe ou pour explorer les alternatives.', + parameters: { + type: 'object', + properties: { + category: { + type: 'string', + description: 'Nom de la categorie.', + nullable: true, + }, + subCategory: { + type: 'string', + description: 'Nom de la sous-categorie.', + nullable: true, + }, + }, + }, + }, + ], + }, +]; + +const SYSTEM_PROMPT = `Tu es un expert logisticien IA pour la gestion de materiel evenementiel. +Tu dois proposer une liste de materiel et de flight cases adaptee a l evenement decrit. + +Regles sur les UNITES et QUANTITES (CRITIQUE) : +- La plupart des equipements (Lumiere, Son, Video, Structure) sont des UNITES UNIQUES. Chaque ID (ex: "BARRE_LED_#1") represente un seul appareil physique. +- Si l'utilisateur demande 6 barres LED, tu dois trouver et ajouter 6 IDs DIFFERENTS (ex: "BARRE_LED_#1", "BARRE_LED_#2", etc.) dans proposal.items, chacun avec "quantity": 1. +- Ne jamais mettre "quantity": 6 pour un item dont totalQuantity est 1 ou null. +- Les seules exceptions sont les categories "CONSUMABLE" (Consommable) et "CABLE" (Cable) qui peuvent avoir une quantite > 1 pour un seul ID. + +Regles sur les flight cases (PRIORITAIRE) : +- Les flight cases (containers) sont le moyen privilege de transporter le materiel. +- Strategie : + 1. Identifie d'abord tous les equipements individuels necessaires. + 2. Appelle search_containers avec la liste de TOUS ces IDs d'equipements. + 3. Si un ou plusieurs flight cases couvrent une partie ou la totalite du materiel : + - Ajoute les flight cases dans "proposal.containers". + - RETIRE les equipements contenus dans ces flight cases de ta liste "proposal.items". + - Les equipements restants (non contenus dans des flight cases) doivent rester dans "proposal.items". +- Un flight case est toujours preferable a des items individuels. + +Regles de recherche (STRATEGIE EN DEUX PASSES) : +1. PREMIER PASSE (RECHERCHE PRECISE) : + - Cherche chaque item demande avec son nom complet (ex: "dB Technologies Ignenia IG5TR"). + - Si ca ne donne rien, REESSAYE immediatement avec des mots-clefs plus courts (ex: "Ignenia", "IG5TR", "VIO S118"). + - Sois perseverant : les noms en base sont souvent au format "MARQUE_MODELE_#1". + +2. DEUXIEME PASSE (ANALYSE CONTEXTUELLE ET CATEGORIELLE) : + - Si la recherche precise echoue, analyse le contexte de l'item dans le devis ou la demande. + - Identifie la categorie probable (ex: "dB Technologies" -> "SONO" ou "SOUND", "Robert Juliat" -> "LUMIERE" ou "LIGHTING"). + - Utilise list_equipment_by_category pour recuperer tout le materiel de cette categorie. + - Parcoure la liste recuperee pour trouver un item qui "match" logiquement (ex: l'utilisateur demande "Opera 15" mais l'identifiant est "DBTE_OPERA_15_#1"). + - Si tu trouves une correspondance logique dans la liste de la categorie, utilise l'ID exact trouve en base. + + Regles de transparence (OBLIGATOIRE) : + - Ton assistantMessage doit TOUJOURS contenir deux sections claires et distinctes si applicable : + 1. "Matériel ajouté :" (Utilise exactement ce titre) suivi de la liste des équipements et flight cases que tu as réussi à trouver et qui sont disponibles. Explique brièvement pourquoi. + 2. "Matériel non trouvé ou indisponible :" (Utilise exactement ce titre) suivi de la liste précise des éléments de la demande utilisateur que tu n'as pas pu intégrer. Pour chaque élément, précise s'il est "Absent de la base de données" ou "Indisponible (rupture de stock)". + - N'hésite pas à être pédagogue dans tes explications, mais garde ces titres de section exacts pour permettre un affichage structuré dans l'application. + + Regles absolues : + - Tu ne dois JAMAIS ecrire en base de donnees. + - Si l utilisateur cite un evenement precis (nom/date), appelle d abord search_event_reference. + - Rend toujours la LISTE FINALE COMPLETE y compris le materiel deja assigne s'il est toujours pertinent. + - Verifie la disponibilite de CHAQUE equipement ET container propose via check_availability_batch. + - Si un materiel est en rupture de stock, cherche une alternative (autre ID du meme modele ou autre modele similaire). Si aucune alternative n'est trouvee, deplace l'item dans la section "indisponible". + +Reponse finale : du JSON valide strict, sans markdown, avec ce format exact : +{"assistantMessage":"...","proposal":{"summary":"...","containers":[{"containerId":"ID-EXACT","rationale":"..."}],"items":[{"equipmentId":"ID-EXACT","quantity":1,"rationale":"..."}]}} +- containers : flight cases proposes. +- items : equipements individuels NON contenus dans les flight cases proposes ci-dessus. +- Si impossible, renvoie proposal a null.`; + +// ============================================================================ +// Implémentation des tools côté serveur (Firestore Admin SDK) +// ============================================================================ +/** + * Recherche des équipements dans Firestore par mot-clé. + */ +async function toolSearchEquipment(query) { + if (!query || query.trim().length < 2) { + return { query, count: 0, results: [] }; + } + + // Normalisation pour comparer proprement + const normalize = (str) => + (str || '') + .toString() + .toLowerCase() + .normalize('NFD') + .replace(/[\u0300-\u036f]/g, '') + .replace(/[^a-z0-9\s-]/g, '') + .trim(); + + const normalizedQuery = normalize(query); + const tokens = extractQueryTokens(query); + + // Charger un snapshot raisonnable une seule fois + const snapshot = await getDb().collection('equipments').limit(500).get(); + + const docs = snapshot.docs.map((doc) => { + const data = doc.data(); + return { + id: doc.id, + name: String(data.name || doc.id), + category: data.category || '', + subCategory: data.subCategory || '', + brand: data.brand || null, + model: data.model || null, + status: data.status || '', + availableQuantity: data.availableQuantity ?? null, + totalQuantity: data.totalQuantity ?? null, + }; + }); + + const results = []; + + // PASS 1 : recherche stricte / exacte (id, nom complet, modele) + for (const item of docs) { + const candText = normalize([item.id, item.name, item.brand, item.model].join(' ')); + if ( + candText === normalizedQuery + || candText.includes(normalizedQuery) + || normalizedQuery.includes(candText) + ) { + results.push(item); + } + } + + // PASS 2 : recherche par tokens (mots-clés, marque, modèle partiel) + if (results.length === 0) { + for (const item of docs) { + const candText = normalize([item.id, item.name, item.brand, item.model].join(' ')); + const tokenMatch = tokens.some((t) => candText.includes(t)); + if (tokenMatch) results.push(item); + } + } + + // PASS 3 : fallback catégoriel si aucune correspondance directe + if (results.length === 0) { + // Essayer d'identifier une marque ou categorie depuis les tokens + const possibleBrand = tokens.length > 0 ? tokens[0] : null; + // Rechercher d'abord par brand + if (possibleBrand) { + for (const item of docs) { + if (item.brand && normalize(String(item.brand)).includes(possibleBrand)) { + results.push(item); + } + } + } + + // Si toujours rien, appeler list_equipment_by_category pour élargir la recherche + if (results.length === 0) { + // tenter de deviner une catégorie depuis la query (prend premier token long) + const guessedCategory = tokens.find((t) => t.length >= 3) || null; + if (guessedCategory) { + try { + const catRes = await toolListEquipmentByCategory(guessedCategory, null); + if (catRes && Array.isArray(catRes.results) && catRes.results.length > 0) { + // essayer un fuzzy match dans la categorie + const normQuery = normalizedQuery; + for (const itm of catRes.results) { + const cand = normalize([itm.id, itm.name, itm.brand, itm.model].join(' ')); + if (cand.includes(normQuery) || tokens.some((t) => cand.includes(t))) { + results.push(itm); + } + } + } + } catch (e) { + // ignore fallback errors + } + } + } + } + + const limited = results.slice(0, SEARCH_RESULTS_LIMIT); + + return { + query, + count: limited.length, + results: limited, + }; +} + +/** + * Vérifie la disponibilité d'un équipement sur une période donnée. + */ +async function toolCheckAvailability(equipmentId, startDate, endDate, excludeEventId, sharedContext) { + if (!equipmentId || !startDate || !endDate) { + return { error: 'equipmentId, startDate et endDate sont requis.' }; + } + + const start = new Date(startDate); + const end = new Date(endDate); + + if (isNaN(start.getTime()) || isNaN(end.getTime())) { + return { error: 'Dates invalides.' }; + } + + const batchResult = await toolCheckAvailabilityBatch( + [equipmentId], + startDate, + endDate, + excludeEventId, + sharedContext, + ); + + if (!batchResult || !Array.isArray(batchResult.results) || batchResult.results.length === 0) { + return { + equipmentId, + available: true, + conflictCount: 0, + conflicts: [], + }; + } + + return batchResult.results[0]; +} + +async function loadAvailabilityCandidates(start, sharedContext) { + const windowKey = start.toISOString().slice(0, 10); + const contextCache = sharedContext?.availabilityCandidatesByWindow; + + if (contextCache && contextCache.has(windowKey)) { + return contextCache.get(windowKey); + } + + let candidateDocs = []; + + try { + const filteredSnapshot = await getDb().collection('events') + .where('EndDateTime', '>=', start) + .orderBy('EndDateTime', 'asc') + .limit(AVAILABILITY_EVENTS_SCAN_LIMIT) + .get(); + candidateDocs = filteredSnapshot.docs; + } catch (error) { + logger.warn('[AI] Availability optimized query failed, fallback to full scan', { + message: error?.message || 'unknown', + }); + + const fallbackSnapshot = await getDb().collection('events').get(); + candidateDocs = fallbackSnapshot.docs; + } + + if (contextCache) { + contextCache.set(windowKey, candidateDocs); + } + + return candidateDocs; +} + +function buildAvailabilityResultForEquipment({ + equipmentId, + start, + end, + candidateDocs, + excludeEventId, +}) { + const conflicts = []; + + for (const eventDoc of candidateDocs) { + if (excludeEventId && eventDoc.id === excludeEventId) { + continue; + } + + const event = eventDoc.data(); + if (isCancelledStatus(event.status)) { + continue; + } + + const eventStart = toDateSafe(event.StartDateTime); + const eventEnd = toDateSafe(event.EndDateTime); + + if (!eventStart || !eventEnd) { + continue; + } + + // Vérifier si l'équipement est assigné à cet événement + const assignedEquipment = Array.isArray(event.assignedEquipment) + ? event.assignedEquipment + : []; + const isDirectlyAssigned = assignedEquipment.some( + (eq) => String(eq.equipmentId || '') === String(equipmentId), + ); + + if (!isDirectlyAssigned) { + continue; + } + + // Vérifier le chevauchement de dates + const hasOverlap = start < eventEnd && end > eventStart; + if (!hasOverlap) { + continue; + } + + const overlapStart = new Date(Math.max(start.getTime(), eventStart.getTime())); + const overlapEnd = new Date(Math.min(end.getTime(), eventEnd.getTime())); + const overlapDays = Math.ceil((overlapEnd - overlapStart) / (1000 * 60 * 60 * 24)); + + conflicts.push({ + eventId: eventDoc.id, + eventName: event.name || event.Name || 'Événement sans nom', + overlapDays: Math.max(overlapDays, 1), + }); + } + + return { + equipmentId, + available: conflicts.length === 0, + conflictCount: conflicts.length, + conflicts, + }; +} + +function buildAvailabilityResultForContainer({ + containerDoc, + containerId, + start, + end, + candidateDocs, + excludeEventId, +}) { + const conflicts = []; + + for (const eventDoc of candidateDocs) { + if (excludeEventId && eventDoc.id === excludeEventId) continue; + + const event = eventDoc.data(); + if (isCancelledStatus(event.status)) continue; + + const eventStart = toDateSafe(event.StartDateTime); + const eventEnd = toDateSafe(event.EndDateTime); + if (!eventStart || !eventEnd) continue; + + // Vérifier si le container est assigné à cet événement + const assignedContainers = Array.isArray(event.assignedContainers) ? event.assignedContainers : []; + const isAssigned = assignedContainers.some((c) => String(c) === String(containerId)); + + // Vérifier chevauchement de dates + const hasOverlap = start < eventEnd && end > eventStart; + if (!hasOverlap) continue; + + if (isAssigned) { + conflicts.push({ + eventId: eventDoc.id, + eventName: event.name || event.Name || 'Événement sans nom', + }); + } + } + + return { + containerId: containerId, + available: conflicts.length === 0, + conflictCount: conflicts.length, + conflicts, + }; +} + +/** + * Vérifie la disponibilité d'un container et son contenu sur une période donnée. + */ +async function toolCheckContainerAvailability(containerId, startDate, endDate, excludeEventId, sharedContext) { + if (!containerId || !startDate || !endDate) { + return { error: 'containerId, startDate et endDate sont requis.' }; + } + + const start = new Date(startDate); + const end = new Date(endDate); + if (isNaN(start.getTime()) || isNaN(end.getTime())) return { error: 'Dates invalides.' }; + + // Charger le container + const containerDocRef = await getDb().collection('containers').doc(containerId).get(); + if (!containerDocRef.exists) { + return { containerId, error: 'Container introuvable.' }; + } + const containerData = containerDocRef.data() || {}; + const equipmentIds = Array.isArray(containerData.equipmentIds) ? containerData.equipmentIds.map((id) => String(id)) : []; + + const candidateDocs = await loadAvailabilityCandidates(start, sharedContext); + + // Vérifier si le container lui-même est assigné + const containerAvailability = buildAvailabilityResultForContainer({ + containerDoc: containerData, + containerId, + start, + end, + candidateDocs, + excludeEventId, + }); + + // Vérifier aussi la disponibilité des équipements internes + let membersAvailability = null; + if (equipmentIds.length > 0) { + const batchRes = await toolCheckAvailabilityBatch(equipmentIds, startDate, endDate, excludeEventId, sharedContext); + membersAvailability = Array.isArray(batchRes.results) ? batchRes.results : []; + } + + // Si un membre est indisponible ou le container est assigné -> indisponible + const anyMemberConflict = Array.isArray(membersAvailability) && membersAvailability.some((m) => m && m.available === false); + const finalAvailable = containerAvailability.available && !anyMemberConflict; + + return { + containerId, + available: finalAvailable, + containerConflicts: containerAvailability.conflicts || [], + memberAvailability: membersAvailability || [], + }; +} + +/** + * Vérifie en batch la disponibilité de plusieurs containers. + */ +async function toolCheckContainerAvailabilityBatch(containerIds, startDate, endDate, excludeEventId, sharedContext) { + if (!Array.isArray(containerIds) || containerIds.length === 0 || !startDate || !endDate) { + return { error: 'containerIds (array), startDate et endDate sont requis.' }; + } + + const normalized = Array.from(new Set(containerIds.map((c) => String(c || '').trim()).filter(Boolean))).slice(0, MAX_BATCH_AVAILABILITY_ITEMS); + if (normalized.length === 0) return { error: 'Aucun containerId valide.' }; + + const results = []; + for (const cid of normalized) { + try { + const r = await toolCheckContainerAvailability(cid, startDate, endDate, excludeEventId, sharedContext); + results.push(r); + } catch (e) { + results.push({ containerId: cid, error: e?.message || 'Erreur interne' }); + } + } + + return { startDate, endDate, count: results.length, results }; +} + +/** + * Vérifie la disponibilité d'une liste d'équipements pour la même période. + */ +async function toolCheckAvailabilityBatch(equipmentIds, startDate, endDate, excludeEventId, sharedContext) { + if (!Array.isArray(equipmentIds) || equipmentIds.length === 0 || !startDate || !endDate) { + return { error: 'equipmentIds (array), startDate et endDate sont requis.' }; + } + + const start = new Date(startDate); + const end = new Date(endDate); + + if (isNaN(start.getTime()) || isNaN(end.getTime())) { + return { error: 'Dates invalides.' }; + } + + const normalizedIds = Array.from(new Set( + equipmentIds + .map((id) => String(id || '').trim()) + .filter((id) => id.length > 0), + )).slice(0, MAX_BATCH_AVAILABILITY_ITEMS); + + if (normalizedIds.length === 0) { + return { error: 'Aucun equipmentId valide.' }; + } + + const candidateDocs = await loadAvailabilityCandidates(start, sharedContext); + + const results = normalizedIds.map((equipmentId) => buildAvailabilityResultForEquipment({ + equipmentId, + start, + end, + candidateDocs, + excludeEventId, + })); + + return { + startDate, + endDate, + count: results.length, + results, + }; +} + +/** + * Retourne les N derniers événements passés similaires avec leur matériel. + */ +async function toolGetPastEvents(eventTypeId) { + const now = new Date(); + let query = getDb().collection('events').orderBy('StartDateTime', 'desc').limit(50); + + if (eventTypeId) { + query = getDb().collection('events') + .where('eventTypeId', '==', eventTypeId) + .orderBy('StartDateTime', 'desc') + .limit(50); + } + + const snapshot = await query.get(); + + const pastEvents = snapshot.docs + .map((doc) => { + const data = doc.data(); + const startDate = data.StartDateTime?.toDate + ? data.StartDateTime.toDate() + : new Date(data.StartDateTime); + return { doc, data, startDate }; + }) + .filter(({ startDate }) => !isNaN(startDate.getTime()) && startDate < now) + .slice(0, PAST_EVENTS_LIMIT) + .map(({ doc, data }) => { + const assignedEquipment = data.assignedEquipment || []; + return { + id: doc.id, + name: data.name || data.Name || 'Sans nom', + startDate: data.StartDateTime?.toDate + ? data.StartDateTime.toDate().toISOString() + : data.StartDateTime, + endDate: data.EndDateTime?.toDate + ? data.EndDateTime.toDate().toISOString() + : data.EndDateTime, + assignedEquipment: assignedEquipment.map((eq) => ({ + equipmentId: eq.equipmentId, + quantity: eq.quantity || 1, + })), + assignedEquipmentCount: assignedEquipment.length, + }; + }); + + return { + count: pastEvents.length, + events: pastEvents, + }; +} + +function toDateSafe(value) { + if (!value) return null; + if (value.toDate && typeof value.toDate === 'function') { + return value.toDate(); + } + const parsed = new Date(value); + return Number.isNaN(parsed.getTime()) ? null : parsed; +} + +function isCancelledStatus(status) { + const normalized = (status || '').toString().trim().toUpperCase(); + return normalized === 'CANCELLED' || normalized === 'CANCELED'; +} + +function normalizeSearchText(value) { + return (value || '') + .toString() + .toLowerCase() + .normalize('NFD') + .replace(/[\u0300-\u036f]/g, '') + .replace(/[^a-z0-9\s-]/g, ' ') + .replace(/\s+/g, ' ') + .trim(); +} + +/** + * Retourne true si le message utilisateur autorise l'utilisation de containers. + * Détecte quelques formulations négatives courantes en français/anglais. + */ +function userAllowsContainers(userMessage) { + if (!userMessage || userMessage.toString().trim().length === 0) return true; + const txt = userMessage.toString().toLowerCase(); + const negativePatterns = [ + 'pas de container', 'sans container', 'pas de flight', 'sans flight', 'pas de flight case', 'sans flight case', + 'ne pas utiliser', 'ne pas prendre', 'do not use', 'no container', 'no flight', 'no flight case' + ]; + return !negativePatterns.some((pat) => txt.includes(pat)); +} + +function parseDateHintFlexible(value) { + const directDate = toDateSafe(value); + if (directDate) { + return directDate; + } + + const normalized = normalizeSearchText(value); + if (!normalized) return null; + + const monthMap = { + janvier: 0, + fevrier: 1, + fevr: 1, + mars: 2, + avril: 3, + avr: 3, + mai: 4, + juin: 5, + juillet: 6, + juil: 6, + aout: 7, + septembre: 8, + sept: 8, + octobre: 9, + oct: 9, + novembre: 10, + nov: 10, + decembre: 11, + dec: 11, + }; + + const frDateMatch = normalized.match(/(\d{1,2})\s+([a-z]+)\s+(\d{4})/); + if (frDateMatch) { + const day = parseInt(frDateMatch[1], 10); + const monthKey = frDateMatch[2]; + const year = parseInt(frDateMatch[3], 10); + const month = monthMap[monthKey]; + + if (!Number.isNaN(day) && !Number.isNaN(year) && month !== undefined) { + const parsed = new Date(Date.UTC(year, month, day)); + return Number.isNaN(parsed.getTime()) ? null : parsed; + } + } + + return null; +} + +function dateToYmd(dateValue) { + if (!dateValue) return null; + return dateValue.toISOString().slice(0, 10); +} + +function dateToYmdInTimezone(dateValue, timeZone) { + if (!dateValue) return null; + + const formatter = new Intl.DateTimeFormat('en-CA', { + timeZone, + year: 'numeric', + month: '2-digit', + day: '2-digit', + }); + + const parts = formatter.formatToParts(dateValue); + const year = parts.find((part) => part.type === 'year')?.value; + const month = parts.find((part) => part.type === 'month')?.value; + const day = parts.find((part) => part.type === 'day')?.value; + + if (!year || !month || !day) { + return null; + } + + return `${year}-${month}-${day}`; +} + +function parseYmdToUtcDate(ymd) { + if (!ymd || !/^\d{4}-\d{2}-\d{2}$/.test(ymd)) { + return null; + } + + const [year, month, day] = ymd.split('-').map((value) => parseInt(value, 10)); + return new Date(Date.UTC(year, month - 1, day)); +} + +function buildToolCacheKey(name, args) { + const safeArgs = args && typeof args === 'object' ? args : {}; + return `${name}:${JSON.stringify(safeArgs)}`; +} + +function isDateMatchingHint(eventDate, hintedYmd) { + if (!eventDate || !hintedYmd) { + return true; + } + + const eventUtcYmd = dateToYmd(eventDate); + const eventParisYmd = dateToYmdInTimezone(eventDate, 'Europe/Paris'); + + if (eventUtcYmd === hintedYmd || eventParisYmd === hintedYmd) { + return true; + } + + const hintedDate = parseYmdToUtcDate(hintedYmd); + if (!hintedDate) { + return false; + } + + const eventDateUtc = parseYmdToUtcDate(eventUtcYmd); + const eventDateParis = parseYmdToUtcDate(eventParisYmd); + const oneDayInMs = 24 * 60 * 60 * 1000; + + const utcDelta = eventDateUtc + ? Math.abs(eventDateUtc.getTime() - hintedDate.getTime()) + : Number.MAX_SAFE_INTEGER; + const parisDelta = eventDateParis + ? Math.abs(eventDateParis.getTime() - hintedDate.getTime()) + : Number.MAX_SAFE_INTEGER; + + return Math.min(utcDelta, parisDelta) <= oneDayInMs; +} + +function extractQueryTokens(rawQuery) { + const monthWords = new Set([ + 'janvier', 'fevrier', 'fevr', 'mars', 'avril', 'avr', 'mai', 'juin', + 'juillet', 'juil', 'aout', 'septembre', 'sept', 'octobre', 'oct', + 'novembre', 'nov', 'decembre', 'dec', + ]); + const stopWords = new Set([ + 'de', 'du', 'des', 'le', 'la', 'les', 'un', 'une', 'en', 'date', 'sur', + 'pour', 'avec', 'et', 'a', 'au', 'aux', + ]); + + return normalizeSearchText(rawQuery) + .split(' ') + .filter((token) => token.length >= 2) + .filter((token) => !/^\d+$/.test(token)) + .filter((token) => !monthWords.has(token)) + .filter((token) => !stopWords.has(token)); +} + +/** + * Recherche des containers/flight cases contenant des equipements donnes. + */ +async function toolSearchContainers(equipmentIds, query) { + if (!Array.isArray(equipmentIds) || equipmentIds.length === 0) { + return { error: 'equipmentIds (array) est requis.' }; + } + + const normalizedEquipmentIds = new Set( + equipmentIds.map((id) => String(id || '').trim()).filter((id) => id.length > 0), + ); + + if (normalizedEquipmentIds.size === 0) { + return { error: 'Aucun equipmentId valide.' }; + } + + const normalizedQuery = query ? normalizeSearchText(query) : null; + + const snapshot = await getDb().collection('containers').limit(300).get(); + const results = []; + + for (const doc of snapshot.docs) { + const data = doc.data(); + if (isCancelledStatus(data.status)) continue; + + const containerEquipmentIds = Array.isArray(data.equipmentIds) + ? data.equipmentIds.map((id) => String(id || '').trim()).filter(Boolean) + : []; + + if (containerEquipmentIds.length === 0) continue; + + const matchingIds = containerEquipmentIds.filter((id) => normalizedEquipmentIds.has(id)); + if (matchingIds.length === 0) continue; + + if (normalizedQuery) { + const searchableText = normalizeSearchText(data.name || doc.id); + if (!searchableText.includes(normalizedQuery)) continue; + } + + results.push({ + id: doc.id, + name: data.name || doc.id, + type: data.type || '', + status: data.status || '', + equipmentIds: containerEquipmentIds, + totalItemCount: containerEquipmentIds.length, + matchingEquipmentIds: matchingIds, + matchingCount: matchingIds.length, + coverageRatio: matchingIds.length / normalizedEquipmentIds.size, + }); + } + + // Trier par meilleure couverture (plus de matching ET ratio le plus proche de 1) + results.sort((a, b) => { + if (b.matchingCount !== a.matchingCount) return b.matchingCount - a.matchingCount; + return b.coverageRatio - a.coverageRatio; + }); + + const limited = results.slice(0, 15); + + return { + requestedEquipmentIds: Array.from(normalizedEquipmentIds), + count: limited.length, + containers: limited, + }; +} + +/** + * Recherche un evenement de reference par nom/date, meme s'il est futur. + */ +async function toolSearchEventReference(query, dateHint) { + const normalizedQuery = normalizeSearchText(query); + if (!normalizedQuery) { + return { query, count: 0, events: [] }; + } + + const hintedDate = parseDateHintFlexible(dateHint) || parseDateHintFlexible(query); + const hintedYmd = hintedDate ? dateToYmd(hintedDate) : null; + const queryTokens = extractQueryTokens(query); + + let docs = []; + + // Priorite a une recherche ciblee autour de la date demandee. + if (hintedDate) { + const dayStart = new Date(Date.UTC( + hintedDate.getUTCFullYear(), + hintedDate.getUTCMonth(), + hintedDate.getUTCDate(), + 0, + 0, + 0, + 0, + )); + const dayEnd = new Date(Date.UTC( + hintedDate.getUTCFullYear(), + hintedDate.getUTCMonth(), + hintedDate.getUTCDate(), + 23, + 59, + 59, + 999, + )); + const rangeStart = new Date(dayStart.getTime() - (24 * 60 * 60 * 1000)); + const rangeEnd = new Date(dayEnd.getTime() + (24 * 60 * 60 * 1000)); + + try { + const byDateSnapshot = await getDb().collection('events') + .where('StartDateTime', '>=', rangeStart) + .where('StartDateTime', '<=', rangeEnd) + .orderBy('StartDateTime', 'desc') + .limit(Math.max(EVENT_SEARCH_SCAN_LIMIT, 150)) + .get(); + docs = byDateSnapshot.docs; + } catch (error) { + logger.warn('[AI] search_event_reference date query failed, fallback to scan', { + message: error?.message || 'unknown', + }); + } + } + + if (docs.length === 0) { + const snapshot = await getDb().collection('events') + .orderBy('StartDateTime', 'desc') + .limit(EVENT_SEARCH_SCAN_LIMIT) + .get(); + docs = snapshot.docs; + } + + const candidates = docs.map((doc) => { + const data = doc.data(); + const startDate = toDateSafe(data.StartDateTime); + const endDate = toDateSafe(data.EndDateTime); + const eventName = (data.name || data.Name || '').toString(); + const searchableText = normalizeSearchText([eventName, doc.id, data.eventTypeId || ''].join(' ')); + + const assignedEquipment = Array.isArray(data.assignedEquipment) ? data.assignedEquipment : []; + const assignedContainers = Array.isArray(data.assignedContainers) ? data.assignedContainers : []; + const matchedTokenCount = queryTokens.filter((token) => searchableText.includes(token)).length; + const hasTokenMatch = queryTokens.length === 0 + ? searchableText.includes(normalizedQuery) + : matchedTokenCount >= Math.min(2, queryTokens.length); + const matchesDate = isDateMatchingHint(startDate, hintedYmd); + const eventYmd = startDate ? dateToYmd(startDate) : null; + + return { + id: doc.id, + name: eventName || 'Sans nom', + startDate, + endDate, + eventYmd, + assignedEquipment, + assignedContainers, + assignedEquipmentCount: assignedEquipment.length, + matchesQuery: hasTokenMatch, + matchedTokenCount, + matchesDate, + }; + }); + + const strictMatched = candidates + .filter((event) => event.matchesQuery && event.matchesDate); + const fallbackMatched = candidates + .filter((event) => event.matchesQuery); + const selectedCandidates = strictMatched.length > 0 ? strictMatched : fallbackMatched; + + const matched = selectedCandidates + .sort((a, b) => { + const aScore = (a.matchesDate ? 3 : 0) + (a.assignedEquipmentCount > 0 ? 1 : 0) + a.matchedTokenCount; + const bScore = (b.matchesDate ? 3 : 0) + (b.assignedEquipmentCount > 0 ? 1 : 0) + b.matchedTokenCount; + if (bScore !== aScore) return bScore - aScore; + + const aTime = a.startDate ? a.startDate.getTime() : 0; + const bTime = b.startDate ? b.startDate.getTime() : 0; + return bTime - aTime; + }) + .slice(0, 5) + .map((event) => ({ + id: event.id, + name: event.name, + startDate: event.startDate ? event.startDate.toISOString() : null, + endDate: event.endDate ? event.endDate.toISOString() : null, + assignedEquipment: event.assignedEquipment.map((eq) => ({ + equipmentId: eq.equipmentId, + quantity: eq.quantity || 1, + })), + assignedContainers: event.assignedContainers, + assignedEquipmentCount: event.assignedEquipmentCount, + })); + + return { + query, + dateHint: hintedYmd || null, + usedDateFallback: strictMatched.length === 0 && Boolean(hintedYmd), + count: matched.length, + events: matched, + }; +} + +/** + * Recherche des équipements par catégorie et sous-catégorie. + */ +async function toolListEquipmentByCategory(category, subCategory) { + let queryDb = getDb().collection('equipments'); + + // Si on a des critères précis, on filtre. Sinon on scanne pour matcher flou. + const snapshot = await queryDb.limit(1000).get(); + + const normalize = (str) => + (str || '').toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g, '').trim(); + + const normCat = category ? normalize(category) : null; + const normSub = subCategory ? normalize(subCategory) : null; + + const results = snapshot.docs + .map((doc) => { + const data = doc.data(); + return { + id: doc.id, + name: data.name || doc.id, + category: data.category || '', + subCategory: data.subCategory || '', + brand: data.brand || null, + model: data.model || null, + status: data.status || '', + }; + }) + .filter(item => { + let match = true; + if (normCat) match = match && normalize(item.category).includes(normCat); + if (normSub) match = match && normalize(item.subCategory).includes(normSub); + return match; + }) + .slice(0, 50); + + return { + category: category || 'all', + subCategory: subCategory || 'all', + count: results.length, + results, + }; +} + +/** + * Exécute un tool Gemini et retourne le résultat. + */ +async function executeTool(toolCall, excludeEventId, sharedContext) { + const { name, args } = toolCall; + logger.info(`[AI] Executing tool: ${name}`, { args }); + + try { + switch (name) { + case 'search_equipment': + return await toolSearchEquipment(args.query); + + case 'check_availability': + return await toolCheckAvailability( + args.equipmentId, + args.startDate, + args.endDate, + excludeEventId, + sharedContext, + ); + + case 'check_availability_batch': + return await toolCheckAvailabilityBatch( + args.equipmentIds, + args.startDate, + args.endDate, + excludeEventId, + sharedContext, + ); + + case 'get_past_events': + return await toolGetPastEvents(args.eventTypeId || null); + + case 'search_event_reference': + return await toolSearchEventReference(args.query, args.dateHint || null); + + case 'search_containers': + return await toolSearchContainers(args.equipmentIds, args.query || null); + + case 'check_container_availability': + return await toolCheckContainerAvailability( + args.containerId, + args.startDate, + args.endDate, + excludeEventId, + sharedContext, + ); + + case 'check_container_availability_batch': + return await toolCheckContainerAvailabilityBatch( + args.containerIds, + args.startDate, + args.endDate, + excludeEventId, + sharedContext, + ); + + case 'list_equipment_by_category': + return await toolListEquipmentByCategory(args.category || null, args.subCategory || null); + + default: + return { error: `Tool inconnu: ${name}` }; + } + } catch (err) { + logger.error(`[AI] Tool error (${name}):`, err); + return { error: err.message }; + } +} + +// ============================================================================ +// Gestionnaire principal +// ============================================================================ +/** + * Construit le prompt utilisateur avec le contexte de l'événement. + */ +function buildUserPrompt({ + userMessage, + eventName, + eventTypeId, + startDate, + endDate, + location, + notes, + eventOptions, + currentEquipment, + workingProposal, +}) { + const currentEquipmentStr = currentEquipment && currentEquipment.length > 0 + ? currentEquipment.map((eq) => `${eq.equipmentId} x${eq.quantity || 1}`).join(', ') + : 'aucun'; + const workingProposalStr = workingProposal && workingProposal.length > 0 + ? workingProposal.map((eq) => `${eq.equipmentId} x${eq.quantity || 1}`).join(', ') + : 'aucune'; + + const isAutoMode = !userMessage || userMessage.trim().length === 0; + const finalMessage = isAutoMode + ? 'Génère automatiquement une proposition de matériel adaptée à cet événement, en tenant compte des options et des détails fournis, et basée sur les événements similaires passés.' + : userMessage.trim(); + + return [ + 'Contexte de l\'événement :', + `- Nom : ${eventName || 'non renseigné'}`, + `- Type d'événement (ID): ${eventTypeId || 'non renseigné'}`, + `- Date de début : ${startDate}`, + `- Date de fin : ${endDate}`, + `- Lieu : ${location || 'non renseigné'}`, + `- Notes/Description : ${notes || 'aucune'}`, + `- Options de l'événement : ${eventOptions || 'aucune'}`, + `- Matériel déjà assigné : ${currentEquipmentStr}`, + `- Proposition courante à modifier : ${workingProposalStr}`, + '', + 'Demande :', + finalMessage, + '', + 'Si un fichier (document/devis) a été joint à ma demande (sous forme inlineData by Gemini), tu dois absolument l analyser, en deduire le materiel demande, et l ajouter a ta proposition.', + 'Si la demande cite un evenement precis (nom/date), commence par search_event_reference avant de proposer du materiel.', + 'Si une proposition courante existe, traite-la comme base de travail et renvoie toujours la liste finale complete apres modification.', + 'Rappel : vérifier la disponibilité avant de recommander. Privilégier check_availability_batch pour contrôler plusieurs équipements en un appel.', + 'En cas d\'indisponibilité, chercher une alternative via search_equipment puis revérifier.', + ].join('\n'); +} + +/** + * Construit un tableau de parts pour Gemini permettant d'inclure des documents. + */ +function buildMessageParts(params, logDebug) { + const textPrompt = buildUserPrompt(params); + const parts = [{ text: textPrompt }]; + + if (params.document && params.document.data && params.document.mimeType) { + if (logDebug) logDebug(`[AI] Attaching document ${params.document.mimeType}`); + else logger.info(`[AI] Attaching document ${params.document.mimeType}`); + parts.push({ + inlineData: { + data: params.document.data, + mimeType: params.document.mimeType, + } + }); + const fileNameInfo = params.document.fileName ? ` (nom du fichier: ${params.document.fileName})` : ``; + parts.push({ + text: `J'ai joint un document de type ${params.document.mimeType}${fileNameInfo}. Merci d'extraire le materiel et de l'ajouter a la proposition.`, + }); + } + + return parts; +} + +/** + * Extrait un texte exploitable depuis la réponse Gemini, même si response.text() est vide. + */ +function extractResponseText(modelResponse, logDebug, logError) { + const info = logDebug || logger.info.bind(logger); + const warn = logError || logger.warn.bind(logger); + + if (!modelResponse) { + warn('[AI] extractResponseText: modelResponse est vide ou undefined'); + return ''; + } + + info('[AI] extractResponseText: analyse de la réponse brut', { + hasTextFunc: typeof modelResponse.text === 'function', + candidatesCount: modelResponse.candidates?.length || 0 + }); + + try { + const directText = modelResponse.text?.(); + if (directText && directText.trim().length > 0) { + info('[AI] extractResponseText: Texte extrait via modelResponse.text()'); + return directText.trim(); + } + } catch (err) { + warn('[AI] extractResponseText: erreur lors de l appel a modelResponse.text()', err?.message); + } + + const candidates = Array.isArray(modelResponse.candidates) ? modelResponse.candidates : []; + const textParts = []; + + for (const candidate of candidates) { + const parts = candidate?.content?.parts; + if (!Array.isArray(parts)) continue; + + for (const part of parts) { + if (typeof part?.text === 'string' && part.text.trim().length > 0) { + textParts.push(part.text.trim()); + } + } + } + + const fallbackText = textParts.join('\n').trim(); + info(`[AI] extractResponseText: Texte extrait en fallback (longueur: ${fallbackText.length})`); + return fallbackText; +} + +/** + * Extrait et parse le JSON de la réponse IA. + */ +function parseAiResponse(rawText, logDebug, logError) { + const info = logDebug || logger.info.bind(logger); + const err = logError || logger.error.bind(logger); + + if (!rawText || rawText.trim().length === 0) { + err('[AI] parseAiResponse: rawText recu est vide !'); + throw new Error('Réponse IA vide.'); + } + + info('[AI] parseAiResponse tentatives de parsing sur le texte (apercu):', rawText.substring(0, 100) + '...'); + // Tentative directe + try { + const parsed = JSON.parse(rawText.trim()); + if (parsed && typeof parsed === 'object') return parsed; + } catch (_) { + // Continuer avec extraction depuis markdown + } + + // Extraction depuis un bloc markdown ```json ... ``` + const fencedMatch = rawText.match(/```(?:json)?\s*([\s\S]*?)```/); + if (fencedMatch) { + try { + const parsed = JSON.parse(fencedMatch[1]); + if (parsed && typeof parsed === 'object') return parsed; + } catch (_) { + // Continuer + } + } + + // Extraction du premier objet JSON brut dans le texte + const jsonMatch = rawText.match(/\{[\s\S]*\}/); + if (jsonMatch) { + try { + const parsed = JSON.parse(jsonMatch[0]); + if (parsed && typeof parsed === 'object') return parsed; + } catch (_) { + // Échoue + } + } + + throw new Error('JSON IA invalide ou introuvable dans la réponse.'); +} + +/** + * Envoie un message à Gemini avec une stratégie de retry robuste. + * Exponential backoff : délais augmentent avec chaque tentative. + * 503 (surcharge) = plus d'attente que 429 (quota). + */ +async function sendMessageWithRetry(chat, messagePayload, logDebug, logError) { + const maxRetries = 8; + const baseDelayMs = 5000; + + for (let attempt = 1; attempt <= maxRetries; attempt++) { + try { + return await chat.sendMessage(messagePayload); + } catch (error) { + const status = error?.status || error?.response?.status; + const errorMessage = error.message || ''; + const isRateLimit = status === 429 || errorMessage.includes('429') || errorMessage.includes('quota'); + const isOverloaded = status === 503 || errorMessage.includes('503') || errorMessage.includes('overloaded') || errorMessage.includes('high demand'); + + if ((isRateLimit || isOverloaded) && attempt < maxRetries) { + // Exponential backoff : 5s * (2 ^ (attempt - 1)) + // Surcharge (503) : attente plus longue car le modèle est saturé + // Quota (429) : attente plus courte car c'est juste une limite de débit + const exponentialMultiplier = Math.pow(2, Math.max(0, attempt - 1)); + let delayMs = isOverloaded + ? baseDelayMs * exponentialMultiplier * 3 // 15s, 30s, 60s, 120s, etc. + : baseDelayMs * exponentialMultiplier; // 5s, 10s, 20s, 40s, etc. + + // Si le serveur indique un délai spécifique, le respecter + const retryMatch = errorMessage.match(/retry in\s+([\d.]+)\s*s/i); + if (retryMatch && !isNaN(parseFloat(retryMatch[1]))) { + delayMs = Math.ceil(parseFloat(retryMatch[1]) * 1000) + 2000; + } + + const reason = isOverloaded ? 'Modèle surchargé (503 - haute demande)' : 'Quota atteint (429)'; + logDebug( + `[AI] ${reason}. Exponential backoff : tentative ${attempt}/${maxRetries}, ` + + `attente de ${Math.round(delayMs / 1000)}s avant nouvelle tentative...`, + { status, attempt, maxRetries } + ); + await new Promise((resolve) => setTimeout(resolve, delayMs)); + } else { + throw error; + } + } + } +} + +/** + * Handler principal de la Cloud Function aiEquipmentProposal. + */ +async function handleAiEquipmentProposal(req, res) { + const debugLogs = []; + const logDebug = (msg, data) => { + const dataStr = data ? ` | ${JSON.stringify(data)}` : ''; + debugLogs.push(`[INFO] ${msg}${dataStr}`); + logger.info(msg, data); + }; + const logError = (msg, data) => { + const dataStr = data ? ` | ${JSON.stringify(data)}` : ''; + debugLogs.push(`[ERROR] ${msg}${dataStr}`); + logger.error(msg, data); + }; + + // Récupérer la valeur de firebase functions:config si disponible + let firebaseConfigKey = null; + try { + const config = functions.config(); + firebaseConfigKey = config.gemini?.api_key || config.gemini?.apikey; + } catch (e) { + // Ignoré + } + + // Évaluer la clé dynamiquement au moment de l'appel + const dynamicApiKey = process.env.GEMINI_API_KEY || firebaseConfigKey || process.env.GOOGLE_AI_API_KEY; + + if (!dynamicApiKey) { + logError('[AI] Configuration manquante : GEMINI_API_KEY / GOOGLE_AI_API_KEY absent'); + res.status(500).json({ + assistantMessage: 'La configuration de l\'IA est incomplète côté serveur. Veuillez configurer la clé Gemini avant de réessayer.', + proposal: null, + debugLogs, + }); + return; + } + + const { + eventName, + eventTypeId, + startDate, + endDate, + location, + notes, + eventOptions, + userMessage, + history = [], + currentEquipment = [], + workingProposal = [], + excludeEventId, + document, + } = req.body.data || {}; + + if (!startDate || !endDate) { + res.status(400).json({ error: 'startDate et endDate sont requis.' }); + return; + } + + const genAI = new GoogleGenerativeAI(dynamicApiKey); + const model = genAI.getGenerativeModel({ + model: GEMINI_MODEL, + systemInstruction: SYSTEM_PROMPT, + tools: AI_TOOLS, + toolConfig: { functionCallingConfig: { mode: 'AUTO' } }, + generationConfig: { temperature: 0.2 }, + }); + + logDebug('[AI] Reconstruction de l historique de conversation. Elements :', history?.length || 0); + + // Reconstruire l'historique de conversation + const chatHistory = (history || []) + .filter((turn) => turn.text && turn.text.trim().length > 0) + .map((turn) => ({ + role: turn.isUser ? 'user' : 'model', + parts: [{ text: turn.text.trim() }], + })); + + logDebug('[AI] Historique formaté :', chatHistory); + + const chat = model.startChat({ history: chatHistory }); + const toolResultCache = new Map(); + const sharedToolContext = { + availabilityCandidatesByWindow: new Map(), + }; + + const messageParts = buildMessageParts({ + userMessage, + eventName, + eventTypeId, + startDate, + endDate, + location, + notes, + eventOptions, + currentEquipment, + workingProposal, + document, + }, logDebug); + + // Sécuriser les logs de base64 + const safeMessagePartsForLogs = JSON.parse(JSON.stringify(messageParts)); + safeMessagePartsForLogs.forEach(part => { + if (part.inlineData && part.inlineData.data) { + part.inlineData.data = ``; + } + }); + + logDebug('[AI] Starting conversation. Parts envoys:', safeMessagePartsForLogs); + + let response; + + try { + response = await sendMessageWithRetry(chat, messageParts, logDebug, logError); + logDebug('[AI] Premiere reponse recue du sendMessage.'); + + // Boucle de function calling avec cache local. + for (let iteration = 0; iteration < MAX_TOOL_ITERATIONS; iteration++) { + const functionCalls = response.response.functionCalls(); + + if (!functionCalls || functionCalls.length === 0) { + logDebug(`[AI] (Iteration ${iteration + 1}) Fin des tool calls, l IA a retourné une réponse (ou un appel vide).`); + break; + } + + logDebug(`[AI] (Iteration ${iteration + 1}) Tool calls demands par l IA:`, functionCalls); + + const callsToProcess = functionCalls.slice(0, MAX_TOOL_CALLS_PER_ITERATION); + const availabilityCalls = callsToProcess.filter( + (call) => call.name === 'check_availability' + && call.args?.equipmentId + && call.args?.startDate + && call.args?.endDate, + ); + + let batchAvailabilityMap = null; + let batchWindow = null; + + if (availabilityCalls.length >= 2) { + const firstStartDate = availabilityCalls[0].args.startDate; + const firstEndDate = availabilityCalls[0].args.endDate; + const hasSameWindow = availabilityCalls.every( + (call) => call.args.startDate === firstStartDate && call.args.endDate === firstEndDate, + ); + + if (hasSameWindow) { + const equipmentIds = Array.from(new Set( + availabilityCalls.map((call) => String(call.args.equipmentId)), + )).sort(); + const batchArgs = { + equipmentIds, + startDate: firstStartDate, + endDate: firstEndDate, + }; + const batchCacheKey = buildToolCacheKey('check_availability_batch', batchArgs); + + let batchResult = toolResultCache.get(batchCacheKey); + if (!batchResult) { + batchResult = await executeTool( + { name: 'check_availability_batch', args: batchArgs }, + excludeEventId, + sharedToolContext, + ); + toolResultCache.set(batchCacheKey, batchResult); + } + + if (batchResult && Array.isArray(batchResult.results)) { + batchAvailabilityMap = new Map( + batchResult.results.map((item) => [String(item.equipmentId), item]), + ); + batchWindow = { startDate: firstStartDate, endDate: firstEndDate }; + logDebug('[AI] Consolidated check_availability calls into one batch call', { + iteration: iteration + 1, + itemCount: equipmentIds.length, + }); + } + } + } + + logDebug(`[AI] Tool calls executes. Preparation du renvoi des resultats...`); + + const toolResults = await Promise.all( + functionCalls.map(async (toolCall, ind) => { + if (ind >= MAX_TOOL_CALLS_PER_ITERATION) { + return { + functionResponse: { + name: toolCall.name, + response: { error: 'Limite de tool calls simultanes atteinte. (Ignore)' }, + }, + }; + } + + const cacheKey = buildToolCacheKey(toolCall.name, toolCall.args); + if (toolResultCache.has(cacheKey)) { + return { + functionResponse: { + name: toolCall.name, + response: toolResultCache.get(cacheKey), + }, + }; + } + + let toolResult; + try { + if ( + batchAvailabilityMap + && batchWindow + && toolCall.name === 'check_availability' + && toolCall.args?.startDate === batchWindow.startDate + && toolCall.args?.endDate === batchWindow.endDate + ) { + toolResult = batchAvailabilityMap.get(String(toolCall.args.equipmentId)) || { + equipmentId: toolCall.args.equipmentId, + available: true, + conflictCount: 0, + conflicts: [], + }; + } else { + toolResult = await executeTool(toolCall, excludeEventId, sharedToolContext); + } + } catch (toolError) { + logError('[AI] Tool call failed, returning degraded tool response', { + tool: toolCall.name, + message: toolError?.message || 'unknown', + }); + toolResult = { error: toolError?.message || `Echec du tool ${toolCall.name}` }; + } + + toolResultCache.set(cacheKey, toolResult); + + logDebug(`[AI] Resultat du tool ${toolCall.name} (apercu):`, JSON.stringify(toolResult).substring(0, 150) + '...'); + + return { + functionResponse: { + name: toolCall.name, + response: toolResult, + }, + }; + }), + ); + + logDebug(`[AI] Envoi de ${toolResults.length} resultats au modèle pour la suite de la conversation.`); + response = await sendMessageWithRetry(chat, toolResults, logDebug, logError); + logDebug(`[AI] Reponse recue du modele suite aux resultats des tools.`); + } + } catch (error) { + logError('[AI] Conversation timeout/error, DETAIL COMPLET:', { + message: error?.message || 'unknown', + status: error?.status, + details: error?.details, + stack: error?.stack + }); + res.status(200).json({ + assistantMessage: 'Je suis désolé, je n\'ai pas réussi à traiter votre demande en raison d\'un temps trop long ou d\'une erreur technique. Veuillez réessayer avec des requêtes plus spécifiques.', + proposal: null, + debugLogs, + }); + return; + } + + logDebug('[AI] Structure finale du response.response (apercu):', JSON.stringify(response.response).substring(0, 300) + '...'); + const rawText = extractResponseText(response.response, logDebug, logError); + logDebug('[AI] Raw response extracted:', { + hasText: rawText.length > 0, + rawTextExcerpt: rawText.substring(0, 200) + '...' + }); + + // Fallback non bloquant: éviter un 500 quand Gemini ne renvoie pas de texte exploitable. + if (!rawText) { + res.status(200).json({ + assistantMessage: 'Je n\'ai pas pu générer une réponse exploitable pour le moment. Réessaie avec une consigne plus précise.', + proposal: null, + debugLogs, + }); + return; + } + + let payload; + try { + payload = parseAiResponse(rawText, logDebug, logError); + } catch (error) { + logError('[AI] JSON parsing failed, returning degraded response', error?.message); + res.status(200).json({ + assistantMessage: rawText, + proposal: null, + debugLogs, + }); + return; + } + + const assistantMessage = payload.assistantMessage?.toString().trim() || rawText; + + // Normaliser la proposition (items + containers) + let proposal = null; + if (payload.proposal) { + const rawItems = Array.isArray(payload.proposal.items) ? payload.proposal.items : []; + const rawContainers = Array.isArray(payload.proposal.containers) ? payload.proposal.containers : []; + + const items = rawItems + .filter((item) => item.equipmentId && item.quantity > 0) + .map((item) => ({ + equipmentId: String(item.equipmentId).trim(), + quantity: Math.max(1, parseInt(item.quantity) || 1), + rationale: (item.rationale || 'Proposition IA').trim(), + })); + + const containers = rawContainers + .filter((c) => c.containerId) + .map((c) => ({ + containerId: String(c.containerId).trim(), + rationale: (c.rationale || 'Proposition IA').trim(), + })); + + if (items.length > 0 || containers.length > 0) { + proposal = { + summary: payload.proposal.summary?.toString().trim() || 'Proposition generee automatiquement.', + items, + containers, + }; + } + } + + // Post-traitement : privilégier les containers si ceux-ci contiennent uniquement des équipements + // demandés par l'événement (i.e. tous les équipements du container sont nécessaires). + try { + if (proposal && Array.isArray(proposal.items) && proposal.items.length > 0) { + const allowContainers = userAllowsContainers(userMessage); + logDebug('[AI] Vérification auto des containers possible (autorisé par l utilisateur) :', { allowContainers }); + + if (allowContainers) { + const itemIds = proposal.items.map((it) => String(it.equipmentId)); + if (itemIds.length > 0) { + const searchArgs = { equipmentIds: itemIds }; + const cacheKey = buildToolCacheKey('search_containers', searchArgs); + let containersResult = toolResultCache.get(cacheKey); + if (!containersResult) { + containersResult = await executeTool({ name: 'search_containers', args: searchArgs }, excludeEventId, sharedToolContext); + toolResultCache.set(cacheKey, containersResult); + } + + if (containersResult && Array.isArray(containersResult.containers) && containersResult.containers.length > 0) { + const itemsSet = new Set(itemIds); + proposal.containers = proposal.containers || []; + + for (const c of containersResult.containers) { + const cEquip = Array.isArray(c.equipmentIds) ? c.equipmentIds.map((id) => String(id)) : []; + if (cEquip.length === 0) continue; + + const matching = Array.isArray(c.matchingEquipmentIds) ? c.matchingEquipmentIds.map((id) => String(id)) : cEquip.filter((id) => itemsSet.has(String(id))); + const missing = cEquip.filter((id) => !itemsSet.has(String(id))); + const isFull = matching.length > 0 && missing.length === 0; + + // Vérifier la disponibilité du container (et de ses membres) pour la période + const checkArgs = { containerId: c.id, startDate: startDate, endDate: endDate }; + const checkKey = buildToolCacheKey('check_container_availability', checkArgs); + let containerAvail = toolResultCache.get(checkKey); + if (!containerAvail) { + try { + containerAvail = await executeTool({ name: 'check_container_availability', args: checkArgs }, excludeEventId, sharedToolContext); + } catch (err) { + containerAvail = { containerId: c.id, available: true, error: err?.message || 'Erreur check' }; + } + toolResultCache.set(checkKey, containerAvail); + } + + const containerEntry = { + containerId: String(c.id), + rationale: `Conteneur ${c.name || c.id} (matching: ${matching.length}/${cEquip.length})`, + equipmentIds: cEquip, + matchingEquipmentIds: matching, + missingEquipmentIds: missing, + matchingCount: matching.length, + totalItemCount: cEquip.length, + coverageRatio: c.coverageRatio || (matching.length / Math.max(1, itemsSet.size)), + partial: !isFull, + available: containerAvail && typeof containerAvail.available === 'boolean' ? containerAvail.available : true, + availabilityDetail: containerAvail || null, + }; + + // Si le container couvre totalement ses éléments -> favoriser et retirer les items individuels + if (isFull) { + const already = proposal.containers.find((pc) => pc.containerId === c.id); + if (!already) { + proposal.containers.push(containerEntry); + logDebug('[AI] Ajout automatique du container car tous ses éléments sont demandés :', { containerId: c.id }); + } + + const covered = new Set(cEquip.map((id) => String(id))); + proposal.items = proposal.items.filter((it) => !covered.has(String(it.equipmentId))); + } else if (matching.length > 0) { + // Container partiel : proposer à l'utilisateur d'ajouter le container complète + proposal.containers.push(containerEntry); + logDebug('[AI] Container partiel proposé (non retiré des items) :', { containerId: c.id, matchingCount: matching.length, missingCount: missing.length }); + } + } + } + } + } + } + } catch (e) { + logError('[AI] Erreur pendant l auto-selection des containers :', { message: e?.message || e }); + } + + res.status(200).json({ assistantMessage, proposal, debugLogs }); +} + +module.exports = { handleAiEquipmentProposal }; diff --git a/em2rp/functions/index.js b/em2rp/functions/index.js index 537a8d6..6aae0f1 100644 --- a/em2rp/functions/index.js +++ b/em2rp/functions/index.js @@ -3,8 +3,11 @@ * Architecture backend sécurisée avec authentification et permissions */ -// Charger les variables d'environnement depuis .env -require('dotenv').config(); +// Charger les variables d'environnement depuis .env.local (développement) +// ou .env (production Firebase) +const path = require('path'); +require('dotenv').config({ path: path.join(__dirname, '.env.local') }); +require('dotenv').config({ path: path.join(__dirname, '.env') }); const { onRequest, onCall } = require("firebase-functions/v2/https"); const { onSchedule } = require("firebase-functions/v2/scheduler"); @@ -17,6 +20,7 @@ const { Storage } = require('@google-cloud/storage'); const auth = require('./utils/auth'); const helpers = require('./utils/helpers'); const { generateTTS } = require('./generateTTS'); +const { handleAiEquipmentProposal } = require('./aiEquipmentProposal'); // Initialisation sécurisée if (!admin.apps.length) { @@ -33,6 +37,13 @@ const httpOptions = { // Version: 2.0 - Ajout de l'invoker public pour résoudre les problèmes CORS }; +// Options dédiées pour les traitements IA potentiellement longs. +const aiHttpOptions = { + ...httpOptions, + timeoutSeconds: 300, + memory: '1GiB', +}; + // ============================================================================ // CORS Middleware // ============================================================================ @@ -4544,3 +4555,20 @@ exports.generateTTSV2 = onRequest(ttsHttpOptions, async (req, res) => { } }); +// ============================================================================ +// AI - Assistant Logisticien (Gemini avec function calling côté serveur) +// ============================================================================ + +exports.aiEquipmentProposal = onRequest(aiHttpOptions, withCors(async (req, res) => { + try { + // Authentification Firebase obligatoire (pas de clé API côté client) + await auth.authenticateUser(req); + await handleAiEquipmentProposal(req, res); + } catch (error) { + logger.error('[aiEquipmentProposal] Error:', error); + if (!res.headersSent) { + res.status(500).json({ error: error.message }); + } + } +})); + diff --git a/em2rp/functions/package-lock.json b/em2rp/functions/package-lock.json index 90587fb..fdbcf48 100644 --- a/em2rp/functions/package-lock.json +++ b/em2rp/functions/package-lock.json @@ -8,11 +8,12 @@ "dependencies": { "@google-cloud/storage": "^7.18.0", "@google-cloud/text-to-speech": "^5.4.0", + "@google/generative-ai": "^0.21.0", "axios": "^1.13.2", "dotenv": "^17.2.3", "envdot": "^0.0.3", "firebase-admin": "^12.6.0", - "firebase-functions": "^7.0.3", + "firebase-functions": "^7.2.5", "handlebars": "^4.7.8", "nodemailer": "^6.10.1" }, @@ -785,6 +786,15 @@ "node": ">=14.0.0" } }, + "node_modules/@google/generative-ai": { + "version": "0.21.0", + "resolved": "https://registry.npmjs.org/@google/generative-ai/-/generative-ai-0.21.0.tgz", + "integrity": "sha512-7XhUbtnlkSEZK15kN3t+tzIMxsbKm/dSkKBFalj+20NvPKe1kBY7mR2P7vuijEn+f06z5+A8bVGKO0v39cr6Wg==", + "license": "Apache-2.0", + "engines": { + "node": ">=18.0.0" + } + }, "node_modules/@grpc/grpc-js": { "version": "1.13.4", "resolved": "https://registry.npmjs.org/@grpc/grpc-js/-/grpc-js-1.13.4.tgz", @@ -3354,9 +3364,9 @@ } }, "node_modules/firebase-functions": { - "version": "7.0.3", - "resolved": "https://registry.npmjs.org/firebase-functions/-/firebase-functions-7.0.3.tgz", - "integrity": "sha512-DiIjIUv0OS4KEAA3jqyIc7ymZKdcmMcaXy7FCCkrDQo/1CVMbDDWMdZIslmsgSjldA2nhau1dTE/6JQI8Urjjw==", + "version": "7.2.5", + "resolved": "https://registry.npmjs.org/firebase-functions/-/firebase-functions-7.2.5.tgz", + "integrity": "sha512-K+pP0AknluAguLRbD96hibyXbnOgwnvd4hkExWdGrxnNCLoj8LBFj08uvJYxyvhsCgYzQumrUaHBW4lsXKSiRg==", "license": "MIT", "peer": true, "dependencies": { @@ -3375,7 +3385,8 @@ "peerDependencies": { "@apollo/server": "^5.2.0", "@as-integrations/express4": "^1.1.2", - "firebase-admin": "^11.10.0 || ^12.0.0 || ^13.0.0" + "firebase-admin": "^11.10.0 || ^12.0.0 || ^13.0.0", + "graphql": "^16.12.0" }, "peerDependenciesMeta": { "@apollo/server": { @@ -3383,6 +3394,9 @@ }, "@as-integrations/express4": { "optional": true + }, + "graphql": { + "optional": true } } }, diff --git a/em2rp/functions/package.json b/em2rp/functions/package.json index 934521e..f4bbf4e 100644 --- a/em2rp/functions/package.json +++ b/em2rp/functions/package.json @@ -16,11 +16,12 @@ "dependencies": { "@google-cloud/storage": "^7.18.0", "@google-cloud/text-to-speech": "^5.4.0", + "@google/generative-ai": "^0.21.0", "axios": "^1.13.2", "dotenv": "^17.2.3", "envdot": "^0.0.3", "firebase-admin": "^12.6.0", - "firebase-functions": "^7.0.3", + "firebase-functions": "^7.2.5", "handlebars": "^4.7.8", "nodemailer": "^6.10.1" }, diff --git a/em2rp/lib/models/event_model.dart b/em2rp/lib/models/event_model.dart index 87f5171..31ff8ad 100644 --- a/em2rp/lib/models/event_model.dart +++ b/em2rp/lib/models/event_model.dart @@ -174,6 +174,7 @@ ReturnStatus returnStatusFromString(String? status) { class EventEquipment { final String equipmentId; // ID de l'équipement final int quantity; // Quantité initiale assignée + final String? rationale; // Explication/Justification (ex: IA alternative) final bool isPrepared; // Validé en préparation final bool isLoaded; // Validé au chargement final bool isUnloaded; // Validé au déchargement @@ -194,6 +195,7 @@ class EventEquipment { EventEquipment({ required this.equipmentId, this.quantity = 1, + this.rationale, this.isPrepared = false, this.isLoaded = false, this.isUnloaded = false, @@ -212,6 +214,7 @@ class EventEquipment { return EventEquipment( equipmentId: map['equipmentId'] ?? '', quantity: map['quantity'] ?? 1, + rationale: map['rationale'], isPrepared: map['isPrepared'] ?? false, isLoaded: map['isLoaded'] ?? false, isUnloaded: map['isUnloaded'] ?? false, @@ -231,6 +234,7 @@ class EventEquipment { return { 'equipmentId': equipmentId, 'quantity': quantity, + 'rationale': rationale, 'isPrepared': isPrepared, 'isLoaded': isLoaded, 'isUnloaded': isUnloaded, @@ -249,6 +253,7 @@ class EventEquipment { EventEquipment copyWith({ String? equipmentId, int? quantity, + String? rationale, bool? isPrepared, bool? isLoaded, bool? isUnloaded, @@ -265,6 +270,7 @@ class EventEquipment { return EventEquipment( equipmentId: equipmentId ?? this.equipmentId, quantity: quantity ?? this.quantity, + rationale: rationale ?? this.rationale, isPrepared: isPrepared ?? this.isPrepared, isLoaded: isLoaded ?? this.isLoaded, isUnloaded: isUnloaded ?? this.isUnloaded, diff --git a/em2rp/lib/services/ai_equipment_assistant_service.dart b/em2rp/lib/services/ai_equipment_assistant_service.dart new file mode 100644 index 0000000..0dc3f00 --- /dev/null +++ b/em2rp/lib/services/ai_equipment_assistant_service.dart @@ -0,0 +1,280 @@ +import 'dart:async'; + +import 'package:em2rp/models/event_model.dart'; +import 'package:em2rp/services/api_service.dart'; +import 'package:em2rp/utils/debug_log.dart'; + +/// Représente un tour de conversation dans le chat. +class AiAssistantChatTurn { + final bool isUser; + final String text; + + const AiAssistantChatTurn({required this.isUser, required this.text}); +} + +/// Document à attacher pour demander à l'IA d'analyser un devis, etc. +class AiEquipmentDocument { + final String base64Data; + final String mimeType; + final String? fileName; + + const AiEquipmentDocument({ + required this.base64Data, + required this.mimeType, + this.fileName, + }); +} + +/// Un item proposé par l'IA dans la liste de matériel. +class AiEquipmentProposalItem { + final String equipmentId; + final int quantity; + final String rationale; + + const AiEquipmentProposalItem({ + required this.equipmentId, + required this.quantity, + required this.rationale, + }); +} + +/// Métadonnées pour un container proposé par l'IA. +class AiEquipmentProposalContainer { + final String containerId; + final String rationale; + final List equipmentIds; + final List matchingEquipmentIds; + final List missingEquipmentIds; + final bool partial; + final bool? available; + final dynamic availabilityDetail; + + const AiEquipmentProposalContainer({ + required this.containerId, + required this.rationale, + this.equipmentIds = const [], + this.matchingEquipmentIds = const [], + this.missingEquipmentIds = const [], + this.partial = false, + this.available, + this.availabilityDetail, + }); +} + +/// Proposition complète retournée par l'IA. +class AiEquipmentProposal { + final String summary; + final List items; + + /// Équipements individuels prêts à être injectés dans l'état local de l'événement. + final List asEventEquipment; + + /// Containers (métadonnées) proposés par l'IA. + final List containers; + + List get containerIds => containers.map((c) => c.containerId).toList(); + + const AiEquipmentProposal({ + required this.summary, + required this.items, + required this.asEventEquipment, + required this.containers, + }); +} + +/// Réponse complète de l'assistant IA (message + proposition optionnelle). +class AiEquipmentAssistantResponse { + final String assistantMessage; + final AiEquipmentProposal? proposal; + final List debugLogs; + + const AiEquipmentAssistantResponse({ + required this.assistantMessage, + this.proposal, + this.debugLogs = const [], + }); +} + +/// Service assistant IA logisticien. +/// Délègue tous les appels Gemini à la Cloud Function [aiEquipmentProposal]. +/// L'authentification Firebase (token Bearer) suffit — aucune clé API côté client. +class AiEquipmentAssistantService { + final ApiService _apiService; + + AiEquipmentAssistantService({ApiService? apiService}) + : _apiService = apiService ?? FirebaseFunctionsApiService(); + + /// Envoie un message et retourne la réponse de l'assistant IA. + Future generateProposal({ + required DateTime startDate, + required DateTime endDate, + required List history, + required String userMessage, + String? eventTypeId, + String? excludeEventId, + List currentAssignedEquipment = const [], + List workingProposalEquipment = const [], + AiEquipmentDocument? document, + }) async { + final payload = { + 'startDate': startDate.toIso8601String(), + 'endDate': endDate.toIso8601String(), + 'userMessage': userMessage.trim(), + 'history': history + .where((turn) => turn.text.trim().isNotEmpty) + .map((turn) => {'isUser': turn.isUser, 'text': turn.text.trim()}) + .toList(), + 'currentEquipment': currentAssignedEquipment + .map((eq) => {'equipmentId': eq.equipmentId, 'quantity': eq.quantity}) + .toList(), + 'workingProposal': workingProposalEquipment + .map((eq) => {'equipmentId': eq.equipmentId, 'quantity': eq.quantity}) + .toList(), + }; + + if (eventTypeId != null) payload['eventTypeId'] = eventTypeId; + if (excludeEventId != null) payload['excludeEventId'] = excludeEventId; + + if (document != null) { + payload['document'] = { + 'mimeType': document.mimeType, + 'data': document.base64Data, + if (document.fileName != null) 'fileName': document.fileName, + }; + } + + try { + DebugLog.info('[AiEquipmentAssistantService] Calling aiEquipmentProposal Cloud Function'); + + final result = await _apiService.call('aiEquipmentProposal', payload); + final assistantMessage = result['assistantMessage']?.toString().trim() ?? ''; + final proposal = _parseProposal(result['proposal']); + + final rawLogs = result['debugLogs']; + final debugLogs = (rawLogs is List) ? rawLogs.map((e) => e.toString()).toList() : []; + + DebugLog.info( + '[AiEquipmentAssistantService] Response received, items: ${proposal?.items.length ?? 0}', + ); + + return AiEquipmentAssistantResponse( + assistantMessage: assistantMessage.isNotEmpty + ? assistantMessage + : 'Je n\'ai pas pu générer de réponse.', + proposal: proposal, + debugLogs: debugLogs, + ); + } on ApiException catch (e) { + DebugLog.error('[AiEquipmentAssistantService] API error', e); + if (e.isUnauthorized) { + throw Exception('Vous n\'êtes pas authentifié. Reconnectez-vous et réessayez.'); + } + throw Exception('Erreur du service IA (${e.statusCode}): ${e.message}'); + } catch (e) { + DebugLog.error('[AiEquipmentAssistantService] Error', e); + rethrow; + } + } + + AiEquipmentProposal? _parseProposal(dynamic rawProposal) { + if (rawProposal == null || rawProposal is! Map) return null; + + final proposalItems = []; + final eventEquipmentList = []; + // legacy containerIds variable removed (we now use containersMeta) + + final rawItems = rawProposal['items']; + if (rawItems is List) { + for (final rawItem in rawItems) { + if (rawItem is! Map) continue; + final item = Map.from(rawItem); + + final equipmentId = item['equipmentId']?.toString().trim() ?? ''; + final quantity = int.tryParse(item['quantity']?.toString() ?? '1') ?? 1; + + if (equipmentId.isEmpty || quantity <= 0) continue; + + final rationale = item['rationale']?.toString().trim() ?? 'Proposition IA'; + + proposalItems.add(AiEquipmentProposalItem( + equipmentId: equipmentId, + quantity: quantity, + rationale: rationale, + )); + eventEquipmentList.add(EventEquipment( + equipmentId: equipmentId, + quantity: quantity, + rationale: rationale, + )); + } + } + + final containersMeta = []; + final rawContainers = rawProposal['containers']; + if (rawContainers is List) { + for (final rawContainer in rawContainers) { + if (rawContainer is String) { + final cid = rawContainer.toString().trim(); + if (cid.isNotEmpty) { + containersMeta.add(AiEquipmentProposalContainer(containerId: cid, rationale: 'Proposition IA')); + } + continue; + } + if (rawContainer is! Map) continue; + final container = Map.from(rawContainer); + final containerId = container['containerId']?.toString().trim() ?? ''; + if (containerId.isEmpty) continue; + + final rationale = container['rationale']?.toString().trim() ?? 'Proposition IA'; + final equipmentIds = []; + final matching = []; + final missing = []; + + if (container['equipmentIds'] is List) { + for (final v in container['equipmentIds']) { + final s = v == null ? null : v.toString().trim(); + if (s != null && s.isNotEmpty) equipmentIds.add(s); + } + } + if (container['matchingEquipmentIds'] is List) { + for (final v in container['matchingEquipmentIds']) { + final s = v == null ? null : v.toString().trim(); + if (s != null && s.isNotEmpty) matching.add(s); + } + } + if (container['missingEquipmentIds'] is List) { + for (final v in container['missingEquipmentIds']) { + final s = v == null ? null : v.toString().trim(); + if (s != null && s.isNotEmpty) missing.add(s); + } + } + + final partial = container['partial'] is bool ? container['partial'] as bool : (missing.isNotEmpty); + final available = container.containsKey('available') ? (container['available'] is bool ? container['available'] as bool : null) : null; + final availabilityDetail = container.containsKey('availabilityDetail') ? container['availabilityDetail'] : null; + + containersMeta.add(AiEquipmentProposalContainer( + containerId: containerId, + rationale: rationale, + equipmentIds: equipmentIds, + matchingEquipmentIds: matching, + missingEquipmentIds: missing, + partial: partial, + available: available, + availabilityDetail: availabilityDetail, + )); + } + } + + if (proposalItems.isEmpty && containersMeta.isEmpty) return null; + + return AiEquipmentProposal( + summary: rawProposal['summary']?.toString().trim().isNotEmpty == true + ? rawProposal['summary'].toString().trim() + : 'Proposition matériel générée automatiquement.', + items: proposalItems, + asEventEquipment: eventEquipmentList, + containers: containersMeta, + ); + } +} diff --git a/em2rp/lib/services/data_service.dart b/em2rp/lib/services/data_service.dart index 5e6f2d8..db6c5ec 100644 --- a/em2rp/lib/services/data_service.dart +++ b/em2rp/lib/services/data_service.dart @@ -553,6 +553,156 @@ class DataService { } } + /// Recherche des équipements pour l'assistant IA avec fallback paginé. + Future>> searchEquipmentsForAssistant({ + required String query, + int limit = 12, + }) async { + final normalizedQuery = query.trim(); + if (normalizedQuery.isEmpty) { + return []; + } + + try { + final quickResults = await quickSearch( + normalizedQuery, + limit: limit, + includeEquipments: true, + includeContainers: false, + ); + + final equipmentResults = quickResults + .where((item) => + (item['type']?.toString().toLowerCase() ?? '') == 'equipment') + .map(_normalizeAssistantEquipment) + .toList(); + + if (equipmentResults.isNotEmpty) { + return equipmentResults; + } + + final paginated = await getEquipmentsPaginated( + limit: limit, + searchQuery: normalizedQuery, + sortBy: 'id', + sortOrder: 'asc', + ); + + final equipments = + paginated['equipments'] as List>? ?? []; + return equipments.map(_normalizeAssistantEquipment).toList(); + } catch (e) { + DebugLog.error('[DataService] Error in searchEquipmentsForAssistant', e); + throw Exception('Erreur lors de la recherche de matériel: $e'); + } + } + + /// Vérifie la disponibilité d'un équipement dans un format normalisé pour l'IA. + Future> checkEquipmentAvailabilityForAssistant({ + required String equipmentId, + required DateTime startDate, + required DateTime endDate, + String? excludeEventId, + }) async { + try { + final result = await checkEquipmentAvailability( + equipmentId: equipmentId, + startDate: startDate, + endDate: endDate, + excludeEventId: excludeEventId, + ); + + final available = result['available'] as bool? ?? true; + final conflicts = (result['conflicts'] as List? ?? const []) + .whereType>() + .map((conflict) { + final eventData = + conflict['eventData'] as Map? ?? const {}; + final eventName = + (eventData['Name'] ?? conflict['eventName'] ?? '').toString(); + return { + 'eventId': conflict['eventId']?.toString() ?? '', + 'eventName': eventName, + 'overlapDays': conflict['overlapDays'] as int? ?? 0, + }; + }).toList(); + + return { + 'equipmentId': equipmentId, + 'available': available, + 'conflictCount': conflicts.length, + 'conflicts': conflicts, + }; + } catch (e) { + DebugLog.error( + '[DataService] Error in checkEquipmentAvailabilityForAssistant', e); + throw Exception('Erreur lors de la vérification de disponibilité: $e'); + } + } + + /// Retourne des événements passés, idéalement filtrés par type d'événement. + Future>> getPastEventsForAssistant({ + String? eventTypeId, + int limit = 10, + }) async { + try { + final now = DateTime.now(); + final events = eventTypeId != null && eventTypeId.isNotEmpty + ? await getEventsByEventType(eventTypeId) + : (await getEvents())['events'] as List>? ?? []; + + final pastEvents = events.where((event) { + final endDate = _parseEventDate(event['EndDateTime']); + return endDate != null && endDate.isBefore(now); + }).toList(); + + pastEvents.sort((a, b) { + final aDate = _parseEventDate(a['StartDateTime']) ?? + DateTime.fromMillisecondsSinceEpoch(0); + final bDate = _parseEventDate(b['StartDateTime']) ?? + DateTime.fromMillisecondsSinceEpoch(0); + return bDate.compareTo(aDate); + }); + + return pastEvents.take(limit).map((event) { + final assignedEquipment = + event['assignedEquipment'] as List? ?? const []; + return { + 'id': event['id']?.toString() ?? '', + 'name': (event['Name'] ?? '').toString(), + 'startDate': event['StartDateTime']?.toString() ?? '', + 'endDate': event['EndDateTime']?.toString() ?? '', + 'assignedEquipment': assignedEquipment, + 'assignedEquipmentCount': assignedEquipment.length, + }; + }).toList(); + } catch (e) { + DebugLog.error('[DataService] Error in getPastEventsForAssistant', e); + throw Exception( + 'Erreur lors de la récupération des événements passés: $e'); + } + } + + Map _normalizeAssistantEquipment(Map item) { + return { + 'id': (item['id'] ?? '').toString(), + 'name': (item['name'] ?? item['id'] ?? '').toString(), + 'category': (item['category'] ?? '').toString(), + 'status': (item['status'] ?? '').toString(), + 'brand': item['brand']?.toString(), + 'model': item['model']?.toString(), + 'availableQuantity': item['availableQuantity'], + 'totalQuantity': item['totalQuantity'], + }; + } + + DateTime? _parseEventDate(dynamic rawValue) { + if (rawValue is String) { + return DateTime.tryParse(rawValue); + } + return null; + } + // ============================================================================ // USER - Current User // ============================================================================ diff --git a/em2rp/lib/views/container_form_page.dart b/em2rp/lib/views/container_form_page.dart index 9e4845c..bd8096d 100644 --- a/em2rp/lib/views/container_form_page.dart +++ b/em2rp/lib/views/container_form_page.dart @@ -7,6 +7,8 @@ import 'package:em2rp/providers/container_provider.dart'; import 'package:em2rp/providers/equipment_provider.dart'; import 'package:em2rp/utils/debug_log.dart'; import 'package:em2rp/utils/id_generator.dart'; +import 'package:em2rp/services/data_service.dart'; +import 'package:em2rp/services/api_service.dart'; class ContainerFormPage extends StatefulWidget { final ContainerModel? container; @@ -650,25 +652,86 @@ class _EquipmentSelectorDialog extends StatefulWidget { class _EquipmentSelectorDialogState extends State<_EquipmentSelectorDialog> { final TextEditingController _searchController = TextEditingController(); + final ScrollController _scrollController = ScrollController(); + final DataService _dataService = DataService(FirebaseFunctionsApiService()); + EquipmentCategory? _filterCategory; String _searchQuery = ''; late Set _tempSelectedIds; - late final Future _loadingFuture; + + final List _paginatedEquipments = []; + bool _isLoadingMore = false; + bool _hasMoreEquipments = true; + String? _lastEquipmentId; @override void initState() { super.initState(); // Créer une copie temporaire des IDs sélectionnés _tempSelectedIds = Set.from(widget.selectedIds); - _loadingFuture = widget.equipmentProvider.loadEquipments(); + _scrollController.addListener(_onScroll); + _loadNextPage(); } @override void dispose() { _searchController.dispose(); + _scrollController.dispose(); super.dispose(); } + void _onScroll() { + if (_isLoadingMore) return; + if (_scrollController.hasClients && + _scrollController.position.pixels >= _scrollController.position.maxScrollExtent - 300) { + if (_hasMoreEquipments) { + _loadNextPage(); + } + } + } + + Future _loadNextPage() async { + if (_isLoadingMore || !_hasMoreEquipments) return; + setState(() => _isLoadingMore = true); + + try { + final result = await _dataService.getEquipmentsPaginated( + limit: 50, + startAfter: _lastEquipmentId, + searchQuery: _searchQuery.isNotEmpty ? _searchQuery : null, + category: _filterCategory != null ? equipmentCategoryToString(_filterCategory!) : null, + sortBy: 'id', + sortOrder: 'asc', + ); + + final newEquipments = (result['equipments'] as List) + .map((data) => EquipmentModel.fromMap(data as Map, data['id'] as String)) + .toList(); + + if (mounted) { + setState(() { + _paginatedEquipments.addAll(newEquipments); + _hasMoreEquipments = result['hasMore'] as bool? ?? false; + _lastEquipmentId = result['lastVisible'] as String?; + _isLoadingMore = false; + }); + } + } catch (e) { + if (mounted) { + setState(() => _isLoadingMore = false); + } + } + } + + Future _reloadData() async { + setState(() { + _paginatedEquipments.clear(); + _lastEquipmentId = null; + _hasMoreEquipments = true; + }); + await _loadNextPage(); + } + @override Widget build(BuildContext context) { return Dialog( @@ -718,6 +781,7 @@ class _EquipmentSelectorDialogState extends State<_EquipmentSelectorDialog> { setState(() { _searchQuery = ''; }); + _reloadData(); }, ) : null, @@ -726,6 +790,7 @@ class _EquipmentSelectorDialogState extends State<_EquipmentSelectorDialog> { setState(() { _searchQuery = value; }); + _reloadData(); }, ), const SizedBox(height: 16), @@ -743,6 +808,7 @@ class _EquipmentSelectorDialogState extends State<_EquipmentSelectorDialog> { setState(() { _filterCategory = null; }); + _reloadData(); }, selectedColor: AppColors.rouge, labelStyle: TextStyle( @@ -761,6 +827,7 @@ class _EquipmentSelectorDialogState extends State<_EquipmentSelectorDialog> { setState(() { _filterCategory = selected ? category : null; }); + _reloadData(); }, selectedColor: AppColors.rouge, labelStyle: TextStyle( @@ -780,7 +847,7 @@ class _EquipmentSelectorDialogState extends State<_EquipmentSelectorDialog> { Container( padding: const EdgeInsets.all(12), decoration: BoxDecoration( - color: AppColors.rouge.withOpacity(0.1), + color: AppColors.rouge.withValues(alpha: 0.1), borderRadius: BorderRadius.circular(8), ), child: Row( @@ -798,90 +865,62 @@ class _EquipmentSelectorDialogState extends State<_EquipmentSelectorDialog> { // Liste des équipements Expanded( - child: FutureBuilder( - future: _loadingFuture, - builder: (context, snapshot) { - if (snapshot.connectionState == ConnectionState.waiting) { - return const Center(child: CircularProgressIndicator()); - } - - if (snapshot.hasError) { - return Center(child: Text('Erreur: ${snapshot.error}')); - } - - var equipment = List.from( - widget.equipmentProvider.allEquipment, - ); - - // Filtrer par catégorie - if (_filterCategory != null) { - equipment = equipment - .where((e) => e.category == _filterCategory) - .toList(); - } - - // Filtrer par recherche - if (_searchQuery.isNotEmpty) { - final query = _searchQuery.toLowerCase(); - equipment = equipment.where((e) { - return e.id.toLowerCase().contains(query) || - (e.brand?.toLowerCase().contains(query) ?? false) || - (e.model?.toLowerCase().contains(query) ?? false); - }).toList(); - } - - if (equipment.isEmpty) { - return const Center( - child: Text('Aucun équipement trouvé'), - ); - } - - return ListView.builder( - itemCount: equipment.length, - itemBuilder: (context, index) { - final item = equipment[index]; - final isSelected = _tempSelectedIds.contains(item.id); - - return CheckboxListTile( - value: isSelected, - onChanged: (selected) { - setState(() { - if (selected == true) { - _tempSelectedIds.add(item.id); - } else { - _tempSelectedIds.remove(item.id); - } - }); - }, - title: Text( - item.id, - style: const TextStyle(fontWeight: FontWeight.bold), - ), - subtitle: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - if (item.brand != null || item.model != null) - Text('${item.brand ?? ''} ${item.model ?? ''}'), - const SizedBox(height: 4), - Text( - _getCategoryLabel(item.category), - style: TextStyle( - fontSize: 12, - color: Colors.grey.shade600, - ), + child: _paginatedEquipments.isEmpty && !_isLoadingMore + ? const Center(child: Text('Aucun équipement trouvé')) + : ListView.builder( + controller: _scrollController, + itemCount: _paginatedEquipments.length + (_isLoadingMore ? 1 : 0), + itemBuilder: (context, index) { + if (index == _paginatedEquipments.length) { + return const Center( + child: Padding( + padding: EdgeInsets.all(8.0), + child: CircularProgressIndicator(), ), - ], - ), - secondary: Icon( - _getCategoryIcon(item.category), - color: AppColors.rouge, - ), - activeColor: AppColors.rouge, - ); - }, - ); - }, - ), + ); + } + + final item = _paginatedEquipments[index]; + final isSelected = _tempSelectedIds.contains(item.id); + + return CheckboxListTile( + value: isSelected, + onChanged: (selected) { + setState(() { + if (selected == true) { + _tempSelectedIds.add(item.id); + } else { + _tempSelectedIds.remove(item.id); + } + }); + }, + title: Text( + item.id, + style: const TextStyle(fontWeight: FontWeight.bold), + ), + subtitle: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + if (item.brand != null || item.model != null) + Text('${item.brand ?? ''} ${item.model ?? ''}'), + const SizedBox(height: 4), + Text( + _getCategoryLabel(item.category), + style: TextStyle( + fontSize: 12, + color: Colors.grey.shade600, + ), + ), + ], + ), + secondary: Icon( + _getCategoryIcon(item.category), + color: AppColors.rouge, + ), + activeColor: AppColors.rouge, + ); + }, + ), ), // Boutons d'action diff --git a/em2rp/lib/views/equipment_form_page.dart b/em2rp/lib/views/equipment_form_page.dart index c4c1cf2..7c0b7bb 100644 --- a/em2rp/lib/views/equipment_form_page.dart +++ b/em2rp/lib/views/equipment_form_page.dart @@ -163,11 +163,11 @@ class _EquipmentFormPageState extends State { TextFormField( controller: _identifierController, decoration: InputDecoration( - labelText: 'Identifiant *', + labelText: 'Identifiant (Laissez vide pour auto-génération) *', border: const OutlineInputBorder(), prefixIcon: const Icon(Icons.tag), - hintText: isEditing ? null : 'Laissez vide pour générer automatiquement', - helperText: isEditing ? 'Non modifiable' : 'Format auto: {Marque4Chars}_{Modèle}', + hintText: isEditing ? null : 'Auto-attribué par défaut', + helperText: isEditing ? 'Non modifiable' : 'Génération auto recommandée basée sur Marque/Modèle', ), enabled: !isEditing, validator: (value) { diff --git a/em2rp/lib/views/event_add_page.dart b/em2rp/lib/views/event_add_page.dart index 4344e6c..2620f95 100644 --- a/em2rp/lib/views/event_add_page.dart +++ b/em2rp/lib/views/event_add_page.dart @@ -77,7 +77,8 @@ class _EventAddEditPageState extends State { return; } - final success = await _controller.submitForm(context, existingEvent: widget.event); + final success = + await _controller.submitForm(context, existingEvent: widget.event); if (success && mounted) { Navigator.of(context).pop(); } @@ -158,21 +159,25 @@ class _EventAddEditPageState extends State { }, child: Scaffold( appBar: AppBar( - title: Text(isEditMode ? 'Modifier un événement' : 'Créer un événement'), + title: Text( + isEditMode ? 'Modifier un événement' : 'Créer un événement'), ), body: Center( child: SingleChildScrollView( child: (isMobile ? Padding( - padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), + padding: const EdgeInsets.symmetric( + horizontal: 16, vertical: 12), child: _buildFormContent(isMobile), ) : Card( elevation: 6, margin: const EdgeInsets.all(24), - shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(18)), + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(18)), child: Padding( - padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 32), + padding: const EdgeInsets.symmetric( + horizontal: 32, vertical: 32), child: _buildFormContent(isMobile), ), )), @@ -186,15 +191,6 @@ class _EventAddEditPageState extends State { Widget _buildFormContent(bool isMobile) { return Consumer( builder: (context, controller, child) { - // Trouver le nom du type d'événement pour le passer au sélecteur d'options - final selectedEventTypeIndex = controller.selectedEventTypeId != null - ? controller.eventTypes.indexWhere((et) => et.id == controller.selectedEventTypeId) - : -1; - final selectedEventType = selectedEventTypeIndex != -1 - ? controller.eventTypes[selectedEventTypeIndex] - : null; - final selectedEventTypeName = selectedEventType?.name; - return Form( key: _formKey, child: Column( @@ -209,18 +205,22 @@ class _EventAddEditPageState extends State { selectedEventTypeId: controller.selectedEventTypeId, startDateTime: controller.startDateTime, endDateTime: controller.endDateTime, - onEventTypeChanged: (typeId) => controller.onEventTypeChanged(typeId, context), + onEventTypeChanged: (typeId) => + controller.onEventTypeChanged(typeId, context), onStartDateTimeChanged: controller.setStartDateTime, onEndDateTimeChanged: controller.setEndDateTime, - onAnyFieldChanged: () {}, // Géré automatiquement par le contrôleur + onAnyFieldChanged: + () {}, // Géré automatiquement par le contrôleur ), const SizedBox(height: 16), OptionSelectorWidget( - eventType: controller.selectedEventTypeId, // Utilise l'ID au lieu du nom + eventType: controller + .selectedEventTypeId, // Utilise l'ID au lieu du nom selectedOptions: controller.selectedOptions, onChanged: controller.setSelectedOptions, onRemove: (optionId) { - final newOptions = List>.from(controller.selectedOptions); + final newOptions = List>.from( + controller.selectedOptions); newOptions.removeWhere((o) => o['id'] == optionId); controller.setSelectedOptions(newOptions); }, @@ -236,6 +236,7 @@ class _EventAddEditPageState extends State { endDate: controller.endDateTime, onChanged: controller.setAssignedEquipment, eventId: widget.event?.id, + eventTypeId: controller.selectedEventTypeId, ), const SizedBox(height: 16), EventDetailsSection( @@ -247,7 +248,8 @@ class _EventAddEditPageState extends State { contactEmailController: controller.contactEmailController, contactPhoneController: controller.contactPhoneController, isMobile: isMobile, - onAnyFieldChanged: () {}, // Géré automatiquement par le contrôleur + onAnyFieldChanged: + () {}, // Géré automatiquement par le contrôleur ), EventStaffAndDocumentsSection( allUsers: controller.allUsers, @@ -290,9 +292,10 @@ class _EventAddEditPageState extends State { } }, onSubmit: _submit, - onSetConfirmed: !isEditMode ? () { - } : null, - onDelete: isEditMode ? _deleteEvent : null, // Ajout du callback de suppression + onSetConfirmed: !isEditMode ? () {} : null, + onDelete: isEditMode + ? _deleteEvent + : null, // Ajout du callback de suppression ), ], ), diff --git a/em2rp/lib/views/widgets/event/equipment_selection_dialog.dart b/em2rp/lib/views/widgets/event/equipment_selection_dialog.dart index 26501ba..a527870 100644 --- a/em2rp/lib/views/widgets/event/equipment_selection_dialog.dart +++ b/em2rp/lib/views/widgets/event/equipment_selection_dialog.dart @@ -222,7 +222,7 @@ class _EquipmentSelectionDialogState extends State { } } - /// Initialise la sélection avec le matériel déjà assigné + /// Initialise la slection avec le matriel dj assign Future _initializeAlreadyAssigned() async { final Map initialSelection = {}; @@ -304,7 +304,7 @@ class _EquipmentSelectionDialogState extends State { try { final result = await _dataService.getEquipmentsPaginated( - limit: 25, + limit: 50, startAfter: _lastEquipmentId, searchQuery: _searchQuery.isNotEmpty ? _searchQuery : null, category: _selectedCategory != null @@ -331,7 +331,7 @@ class _EquipmentSelectionDialogState extends State { DebugLog.info( '[EquipmentSelectionDialog] Loaded ${newEquipments.length} equipments, total: ${_paginatedEquipments.length}, hasMore: $_hasMoreEquipments'); - // Charger les quantités pour les consommables/câbles de cette page + // Charger les quantites pour les consommables/cbles de cette page await _loadAvailableQuantities(newEquipments); // Si la liste ne peut pas scroller, précharger la page suivante. @@ -354,7 +354,7 @@ class _EquipmentSelectionDialogState extends State { try { final result = await _dataService.getContainersPaginated( - limit: 25, + limit: 50, startAfter: _lastContainerId, searchQuery: _searchQuery.isNotEmpty ? _searchQuery : null, category: _selectedCategory?.name, // Filtre par catégorie d'équipements @@ -421,7 +421,7 @@ class _EquipmentSelectionDialogState extends State { DebugLog.info( '[EquipmentSelectionDialog] Cached ${allEquipmentsToCache.length} equipment(s) from containers, total cache: ${_cachedEquipment.length}'); - // Mettre à jour les statuts de conflit pour les nouveaux containers + // Mettre jour les statuts de conflit pour les nouveaux containers await _updateContainerConflictStatus(); // Si la liste ne peut pas scroller, précharger la page suivante. @@ -454,6 +454,40 @@ class _EquipmentSelectionDialogState extends State { } } + void _checkIfMoreItemsNeeded() { + if (!mounted || _isLoadingMore) return; + + int visibleItems = 0; + if (_displayType == SelectionType.equipment) { + visibleItems = _paginatedEquipments.where((eq) { + return _showConflictingItems || !_conflictingEquipmentIds.contains(eq.id); + }).length; + + if (visibleItems < 15 && _hasMoreEquipments) { + _loadNextEquipmentPage(); + } else if (_scrollController.hasClients && _scrollController.position.maxScrollExtent <= 0 && _hasMoreEquipments) { + _loadNextEquipmentPage(); + } + } else { + visibleItems = _paginatedContainers.where((container) { + if (!_showConflictingItems) { + if (_conflictingContainerIds.contains(container.id)) return false; + final hasConflictingChildren = container.equipmentIds.any( + (eqId) => _conflictingEquipmentIds.contains(eqId), + ); + if (hasConflictingChildren) return false; + } + return true; + }).length; + + if (visibleItems < 15 && _hasMoreContainers) { + _loadNextContainerPage(); + } else if (_scrollController.hasClients && _scrollController.position.maxScrollExtent <= 0 && _hasMoreContainers) { + _loadNextContainerPage(); + } + } + } + @override void dispose() { _searchController.dispose(); @@ -1539,7 +1573,8 @@ class _EquipmentSelectionDialogState extends State { ), ), ), - ], + ], + ), ); }, ); diff --git a/em2rp/lib/views/widgets/event_form/ai_equipment_assistant_dialog.dart b/em2rp/lib/views/widgets/event_form/ai_equipment_assistant_dialog.dart new file mode 100644 index 0000000..6cd77cb --- /dev/null +++ b/em2rp/lib/views/widgets/event_form/ai_equipment_assistant_dialog.dart @@ -0,0 +1,755 @@ +import 'package:em2rp/models/event_model.dart'; +import 'package:em2rp/services/ai_equipment_assistant_service.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:file_picker/file_picker.dart'; +import 'dart:convert'; + +/// Résultat retourné par le dialog après confirmation de la proposition IA. +class AiProposalResult { + final List equipment; + final List containerIds; + + const AiProposalResult({ + required this.equipment, + required this.containerIds, + }); +} + +class AiEquipmentAssistantDialog extends StatefulWidget { + final DateTime startDate; + final DateTime endDate; + final String? eventTypeId; + final String? excludeEventId; + final List currentAssignedEquipment; + + const AiEquipmentAssistantDialog({ + super.key, + required this.startDate, + required this.endDate, + required this.currentAssignedEquipment, + this.eventTypeId, + this.excludeEventId, + }); + + @override + State createState() => + _AiEquipmentAssistantDialogState(); +} + +class _AiEquipmentAssistantDialogState + extends State { + final TextEditingController _messageController = TextEditingController(); + final ScrollController _scrollController = ScrollController(); + final ScrollController _proposalScrollController = ScrollController(); + final List<_AssistantChatMessage> _messages = []; + + late final AiEquipmentAssistantService _assistantService; + + bool _isLoading = false; + String? _errorMessage; + AiEquipmentProposal? _latestProposal; + late List _workingEquipment; + AiEquipmentDocument? _selectedDocument; + List _sessionLogs = []; + Set _selectedContainerIds = {}; + + @override + void initState() { + super.initState(); + _assistantService = AiEquipmentAssistantService(); + _workingEquipment = List.from(widget.currentAssignedEquipment); + } + + @override + void dispose() { + _messageController.dispose(); + _scrollController.dispose(); + _proposalScrollController.dispose(); + super.dispose(); + } + + bool get _isChatEmpty => _messages.isEmpty; + + String get _actionButtonLabel { + return _isChatEmpty ? 'Generer la liste automatiquement' : 'Envoyer'; + } + + Future _sendMessage() async { + if (_isLoading) { + return; + } + + final rawInput = _messageController.text.trim(); + final isAutoMode = _isChatEmpty; + final userMessage = isAutoMode + ? (rawInput.isNotEmpty + ? rawInput + : 'Genere automatiquement une proposition de materiel pour cet evenement.') + : rawInput; + + if (userMessage.isEmpty) { + return; + } + + _messageController.clear(); + setState(() { + _errorMessage = null; + _messages.add(_AssistantChatMessage.user(userMessage)); + if (_selectedDocument != null) { + _messages.add(_AssistantChatMessage.user('[Document joint : ${_selectedDocument!.fileName ?? "Document"}]')); + } + _isLoading = true; + }); + + _scrollToBottom(); + + try { + final documentToSend = _selectedDocument; + _selectedDocument = null; // Clear after sending + final response = await _assistantService + .generateProposal( + startDate: widget.startDate, + endDate: widget.endDate, + eventTypeId: widget.eventTypeId, + excludeEventId: widget.excludeEventId, + currentAssignedEquipment: widget.currentAssignedEquipment, + workingProposalEquipment: _workingEquipment, + userMessage: userMessage, + document: documentToSend, + history: _messages + .map((message) => AiAssistantChatTurn( + isUser: message.isUser, text: message.text)) + .toList(), + ); + + if (!mounted) { + return; + } + + setState(() { + _messages + .add(_AssistantChatMessage.assistant(response.assistantMessage)); + _latestProposal = response.proposal; + if (response.proposal != null) { + _workingEquipment = List.from( + response.proposal!.asEventEquipment, + ); + // Préselectionner les containers non partiels + _selectedContainerIds = { + for (final c in response.proposal!.containers) + if (!c.partial) c.containerId + }; + } + _sessionLogs.addAll(response.debugLogs); + _isLoading = false; + }); + _scrollToBottom(); + } on FormatException catch (error) { + if (!mounted) { + return; + } + setState(() { + _isLoading = false; + _errorMessage = 'Reponse IA invalide: ${error.message}'; + }); + } catch (error) { + if (!mounted) { + return; + } + setState(() { + _isLoading = false; + _errorMessage = 'Erreur IA: $error'; + }); + } + } + + void _scrollToBottom() { + WidgetsBinding.instance.addPostFrameCallback((_) { + if (!_scrollController.hasClients) { + return; + } + _scrollController.animateTo( + _scrollController.position.maxScrollExtent, + duration: const Duration(milliseconds: 250), + curve: Curves.easeOut, + ); + }); + } + + Future _pickDocument() async { + try { + final result = await FilePicker.platform.pickFiles( + type: FileType.custom, + allowedExtensions: ['pdf', 'txt', 'jpg', 'jpeg', 'png'], + withData: true, + ); + + if (result != null && result.files.isNotEmpty) { + final file = result.files.first; + if (file.bytes != null) { + final base64String = base64Encode(file.bytes!); + String mimeType = 'application/octet-stream'; + if (file.extension == 'pdf') mimeType = 'application/pdf'; + else if (file.extension == 'txt') mimeType = 'text/plain'; + else if (file.extension == 'jpg' || file.extension == 'jpeg') mimeType = 'image/jpeg'; + else if (file.extension == 'png') mimeType = 'image/png'; + + setState(() { + _selectedDocument = AiEquipmentDocument( + base64Data: base64String, + mimeType: mimeType, + fileName: file.name, + ); + }); + } + } + } catch (e) { + if (mounted) { + setState(() { + _errorMessage = 'Erreur lors de la selection du document : $e'; + }); + } + } + } + + void _showLogsDialog() { + showDialog( + context: context, + builder: (context) { + return AlertDialog( + title: const Text('Logs de l\'IA'), + content: SizedBox( + width: 800, + height: 600, + child: ListView.builder( + itemCount: _sessionLogs.length, + itemBuilder: (context, index) { + final log = _sessionLogs[index]; + final isError = log.startsWith('[ERROR]'); + return Padding( + padding: const EdgeInsets.symmetric(vertical: 4), + child: Text( + log, + style: TextStyle( + fontFamily: 'monospace', + fontSize: 12, + color: isError ? Colors.red : Colors.black87, + ), + ), + ); + }, + ), + ), + actions: [ + TextButton( + onPressed: () { + final fullLogs = _sessionLogs.join('\n'); + Clipboard.setData(ClipboardData(text: fullLogs)); + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('Logs copiés dans le presse-papiers')), + ); + }, + child: const Text('Copier tout'), + ), + TextButton( + onPressed: () => Navigator.of(context).pop(), + child: const Text('Fermer'), + ), + ], + ); + }, + ); + } + + @override + Widget build(BuildContext context) { + return Dialog( + insetPadding: const EdgeInsets.symmetric(horizontal: 24, vertical: 24), + child: SizedBox( + width: 760, + height: 640, + child: Column( + children: [ + AppBar( + automaticallyImplyLeading: false, + title: const Text('(BETA) Assistant IA Logisticien'), + actions: [ + if (_sessionLogs.isNotEmpty) + IconButton( + icon: const Icon(Icons.bug_report), + tooltip: 'Voir les logs', + onPressed: _showLogsDialog, + ), + IconButton( + icon: const Icon(Icons.close), + onPressed: + _isLoading ? null : () => Navigator.of(context).pop(), + ), + ], + ), + Expanded( + child: Column( + children: [ + Expanded( + child: Container( + color: Colors.grey.shade50, + child: ListView.builder( + controller: _scrollController, + padding: const EdgeInsets.all(16), + itemCount: _messages.length, + itemBuilder: (context, index) { + final message = _messages[index]; + return _buildMessageBubble(message); + }, + ), + ), + ), + if (_isLoading) + const Padding( + padding: + EdgeInsets.symmetric(horizontal: 16, vertical: 8), + child: Row( + children: [ + SizedBox( + width: 18, + height: 18, + child: CircularProgressIndicator(strokeWidth: 2), + ), + SizedBox(width: 12), + Expanded( + child: const Text( + 'Generation en cours... verification du materiel et disponibilites. (Cela peut prendre jusqu\'a une minute en cas de forte affluence)', + textAlign: TextAlign.left, + ), + ), + ], + ), + ), + if (_errorMessage != null) + Container( + width: double.infinity, + margin: const EdgeInsets.fromLTRB(16, 8, 16, 0), + padding: const EdgeInsets.all(12), + decoration: BoxDecoration( + color: Colors.red.shade50, + borderRadius: BorderRadius.circular(10), + border: Border.all(color: Colors.red.shade200), + ), + child: Text( + _errorMessage!, + style: TextStyle(color: Colors.red.shade800), + ), + ), + if (_latestProposal != null) + _buildProposalSummary(_latestProposal!), + if (_selectedDocument != null) + Padding( + padding: const EdgeInsets.only(left: 16, right: 16, top: 8), + child: Row( + children: [ + const Icon(Icons.attach_file, color: Colors.blue, size: 20), + const SizedBox(width: 8), + Expanded( + child: Text( + _selectedDocument!.fileName ?? 'Document joint', + style: const TextStyle(color: Colors.blue, fontWeight: FontWeight.w500), + maxLines: 1, + overflow: TextOverflow.ellipsis, + ), + ), + IconButton( + icon: const Icon(Icons.close, size: 20), + onPressed: () { + setState(() { + _selectedDocument = null; + }); + }, + tooltip: 'Retirer le document', + ), + ], + ), + ), + Padding( + padding: const EdgeInsets.all(16), + child: Row( + children: [ + IconButton( + icon: const Icon(Icons.attach_file), + onPressed: _isLoading ? null : _pickDocument, + tooltip: 'Joindre un devis ou document', + ), + Expanded( + child: TextField( + controller: _messageController, + enabled: !_isLoading, + minLines: 1, + maxLines: 3, + decoration: const InputDecoration( + hintText: + 'Precisez votre besoin (style, jauge, contraintes...)', + border: OutlineInputBorder(), + ), + onSubmitted: (_) => _sendMessage(), + ), + ), + const SizedBox(width: 12), + ElevatedButton( + onPressed: _isLoading ? null : _sendMessage, + child: Text(_actionButtonLabel), + ), + ], + ), + ), + ], + ), + ), + ], + ), + ), + ); + } + + Widget _buildMessageBubble(_AssistantChatMessage message) { + final bubbleColor = message.isUser ? Colors.blue.shade600 : Colors.white; + final textColor = message.isUser ? Colors.white : Colors.black87; + + return Align( + alignment: message.isUser ? Alignment.centerRight : Alignment.centerLeft, + child: Container( + margin: const EdgeInsets.only(bottom: 10), + constraints: const BoxConstraints(maxWidth: 560), + padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), + decoration: BoxDecoration( + color: bubbleColor, + borderRadius: BorderRadius.only( + topLeft: const Radius.circular(16), + topRight: const Radius.circular(16), + bottomLeft: Radius.circular(message.isUser ? 16 : 4), + bottomRight: Radius.circular(message.isUser ? 4 : 16), + ), + boxShadow: [ + BoxShadow( + color: Colors.black.withValues(alpha: 0.05), + blurRadius: 10, + offset: const Offset(0, 4), + ), + ], + border: + message.isUser ? null : Border.all(color: Colors.grey.shade200), + ), + child: message.isUser + ? Text(message.text, style: TextStyle(color: textColor)) + : _buildAssistantMessageContent(message.text), + ), + ); + } + + Widget _buildAssistantMessageContent(String text) { + // Si le message semble structuré par l'IA avec nos nouvelles règles + if (text.contains('Matériel ajouté :') || text.contains('Matériel non trouvé')) { + final sections = text.split('\n\n'); + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: sections.map((section) { + final isAdded = section.contains('Matériel ajouté :'); + final isMissing = section.contains('Matériel non trouvé'); + + if (isAdded) { + return _buildStatusSection( + title: section.split('\n').first, + content: section.split('\n').skip(1).join('\n'), + icon: Icons.check_circle_outline, + color: Colors.green.shade700, + bgColor: Colors.green.shade50, + ); + } else if (isMissing) { + return _buildStatusSection( + title: section.split('\n').first, + content: section.split('\n').skip(1).join('\n'), + icon: Icons.warning_amber_rounded, + color: Colors.orange.shade800, + bgColor: Colors.orange.shade50, + ); + } + return Padding( + padding: const EdgeInsets.only(bottom: 8.0), + child: Text(section), + ); + }).toList(), + ); + } + + return Text(text); + } + + Widget _buildStatusSection({ + required String title, + required String content, + required IconData icon, + required Color color, + required Color bgColor, + }) { + return Container( + width: double.infinity, + margin: const EdgeInsets.only(bottom: 12), + padding: const EdgeInsets.all(12), + decoration: BoxDecoration( + color: bgColor, + borderRadius: BorderRadius.circular(8), + border: Border.all(color: color.withValues(alpha: 0.3)), + ), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + children: [ + Icon(icon, size: 18, color: color), + const SizedBox(width: 8), + Expanded( + child: Text( + title.replaceAll(':', '').trim(), + style: TextStyle( + fontWeight: FontWeight.bold, + color: color, + ), + ), + ), + ], + ), + if (content.trim().isNotEmpty) ...[ + const SizedBox(height: 8), + Text( + content.trim(), + style: TextStyle(fontSize: 13, color: Colors.grey.shade800), + ), + ], + ], + ), + ); + } + + void _confirmProposal({bool excludeAlternatives = false}) { + if (_latestProposal == null) return; + + List equipment = List.from(_latestProposal!.asEventEquipment); + // Ne renvoyer que les containerIds sélectionnés (par défaut les containers complets) + final List containerIds = _selectedContainerIds.isNotEmpty + ? _selectedContainerIds.toList() + : List.from(_latestProposal!.containerIds); + + if (excludeAlternatives) { + // On utilise la liste des items d'origine pour savoir lesquels exclure + // car ils contiennent le champ rationale (avant conversion en EventEquipment) + final idsToExclude = _latestProposal!.items + .where((item) { + final rationale = item.rationale.toLowerCase(); + return rationale.contains('alternative') || + rationale.contains('remplacement') || + rationale.contains('indisponible'); + }) + .map((item) => item.equipmentId) + .toSet(); + + equipment = equipment.where((eq) => !idsToExclude.contains(eq.equipmentId)).toList(); + } + + Navigator.of(context).pop( + AiProposalResult( + equipment: equipment, + containerIds: containerIds, + ), + ); + } + + Widget _buildProposalSummary(AiEquipmentProposal proposal) { + return Container( + width: double.infinity, + margin: const EdgeInsets.fromLTRB(16, 8, 16, 0), + padding: const EdgeInsets.all(16), + constraints: const BoxConstraints(maxHeight: 280), + decoration: BoxDecoration( + color: Colors.white, + borderRadius: BorderRadius.circular(12), + border: Border.all(color: Colors.grey.shade300), + boxShadow: [ + BoxShadow( + color: Colors.black.withValues(alpha: 0.05), + blurRadius: 10, + offset: const Offset(0, 4), + ), + ], + ), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + children: [ + const Icon(Icons.assignment_turned_in, color: Colors.indigo), + const SizedBox(width: 12), + const Expanded( + child: Text( + 'Récapitulatif de la proposition IA', + style: TextStyle( + fontWeight: FontWeight.bold, + fontSize: 16, + color: Colors.indigo, + ), + ), + ), + ], + ), + const SizedBox(height: 12), + Flexible( + child: Scrollbar( + controller: _proposalScrollController, + thumbVisibility: true, + child: SingleChildScrollView( + controller: _proposalScrollController, + padding: const EdgeInsets.only(right: 8), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisSize: MainAxisSize.min, + children: [ + Text( + proposal.summary, + style: const TextStyle(fontStyle: FontStyle.italic), + ), + if (proposal.items.isNotEmpty) ...[ + const SizedBox(height: 12), + const Text( + 'Matériel individuel :', + style: TextStyle(fontWeight: FontWeight.bold, fontSize: 13), + ), + const SizedBox(height: 4), + ...proposal.items.map((item) { + final isAlt = item.rationale.toLowerCase().contains('alternative') || item.rationale.toLowerCase().contains('remplacement'); + return Padding( + padding: const EdgeInsets.only(bottom: 6, left: 4), + child: Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Icon( + isAlt ? Icons.swap_horiz : Icons.add_circle_outline, + size: 14, + color: isAlt ? Colors.orange : Colors.indigo, + ), + const SizedBox(width: 8), + Expanded( + child: Text( + '${item.equipmentId} x${item.quantity}', + style: const TextStyle(fontWeight: FontWeight.w500) + ), + ), + ], + ), + ); + }), + ], + if (proposal.containers.isNotEmpty) ...[ + const SizedBox(height: 12), + const Text( + 'Fly-cases & Boîtes :', + style: TextStyle(fontWeight: FontWeight.bold, fontSize: 13), + ), + const SizedBox(height: 4), + ...proposal.containers.map((c) { + final isPartial = c.partial; + final isSelected = _selectedContainerIds.contains(c.containerId); + return Padding( + padding: const EdgeInsets.only(bottom: 6, left: 4), + child: Row( + children: [ + Icon( + Icons.inventory_2_outlined, + size: 14, + color: c.available == false ? Colors.red : Colors.indigo, + ), + const SizedBox(width: 8), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + children: [ + Expanded(child: Text('${c.containerId} ${c.rationale.isNotEmpty ? "- ${c.rationale}" : ""}', style: const TextStyle(fontWeight: FontWeight.w500))), + if (c.available == false) + Padding( + padding: const EdgeInsets.only(left: 8.0), + child: Icon(Icons.block, color: Colors.red.shade700, size: 14), + ), + ], + ), + if (isPartial) ...[ + const SizedBox(height: 4), + Text('Contenu partiel : ${c.matchingEquipmentIds.length}/${c.equipmentIds.length} items utilisés.', style: TextStyle(fontSize: 12, color: Colors.grey.shade700)), + ], + ], + ), + ), + const SizedBox(width: 8), + if (isPartial) + Checkbox( + value: isSelected, + onChanged: (v) { + setState(() { + if (v == true) _selectedContainerIds.add(c.containerId); + else _selectedContainerIds.remove(c.containerId); + }); + }, + ), + ], + ), + ); + }), + ], + ], + ), + ), + ), + ), + const SizedBox(height: 12), + Wrap( + spacing: 12, + runSpacing: 8, + children: [ + ElevatedButton.icon( + onPressed: _isLoading ? null : () => _confirmProposal(), + icon: const Icon(Icons.check), + label: const Text('Tout ajouter'), + style: ElevatedButton.styleFrom( + backgroundColor: Colors.indigo, + foregroundColor: Colors.white, + ), + ), + OutlinedButton.icon( + onPressed: _isLoading ? null : () => _confirmProposal(excludeAlternatives: true), + icon: const Icon(Icons.filter_list_off), + label: const Text('Ajouter sans alternatives'), + style: OutlinedButton.styleFrom( + foregroundColor: Colors.indigo, + side: const BorderSide(color: Colors.indigo), + ), + ), + ], + ), + ], + ), + ); + } +} + +class _AssistantChatMessage { + final bool isUser; + final String text; + + const _AssistantChatMessage._({required this.isUser, required this.text}); + + factory _AssistantChatMessage.user(String text) { + return _AssistantChatMessage._(isUser: true, text: text); + } + + factory _AssistantChatMessage.assistant(String text) { + return _AssistantChatMessage._(isUser: false, text: text); + } +} diff --git a/em2rp/lib/views/widgets/event_form/event_assigned_equipment_section.dart b/em2rp/lib/views/widgets/event_form/event_assigned_equipment_section.dart index c10eb82..50d691f 100644 --- a/em2rp/lib/views/widgets/event_form/event_assigned_equipment_section.dart +++ b/em2rp/lib/views/widgets/event_form/event_assigned_equipment_section.dart @@ -8,6 +8,7 @@ import 'package:em2rp/providers/equipment_provider.dart'; import 'package:em2rp/providers/container_provider.dart'; import 'package:em2rp/utils/colors.dart'; import 'package:em2rp/views/widgets/event/equipment_selection_dialog.dart'; +import 'package:em2rp/views/widgets/event_form/ai_equipment_assistant_dialog.dart'; /// Section pour afficher et gérer le matériel assigné à un événement class EventAssignedEquipmentSection extends StatefulWidget { @@ -17,6 +18,7 @@ class EventAssignedEquipmentSection extends StatefulWidget { final DateTime? endDate; final Function(List, List) onChanged; final String? eventId; // Pour exclure l'événement actuel de la vérification + final String? eventTypeId; const EventAssignedEquipmentSection({ super.key, @@ -26,14 +28,18 @@ class EventAssignedEquipmentSection extends StatefulWidget { required this.endDate, required this.onChanged, this.eventId, + this.eventTypeId, }); @override - State createState() => _EventAssignedEquipmentSectionState(); + State createState() => + _EventAssignedEquipmentSectionState(); } -class _EventAssignedEquipmentSectionState extends State { - bool get _canAddMaterial => widget.startDate != null && widget.endDate != null; +class _EventAssignedEquipmentSectionState + extends State { + bool get _canAddMaterial => + widget.startDate != null && widget.endDate != null; final Map _equipmentCache = {}; final Map _containerCache = {}; bool _isLoading = true; @@ -61,19 +67,24 @@ class _EventAssignedEquipmentSectionState extends State(); final containerProvider = context.read(); - DebugLog.info('[EventAssignedEquipmentSection] Loading caches from assigned lists'); + DebugLog.info( + '[EventAssignedEquipmentSection] Loading caches from assigned lists'); // Toujours partir des données locales du formulaire pour éviter les décalages visuels. - final equipmentIds = widget.assignedEquipment.map((eq) => eq.equipmentId).toList(); - final containers = await containerProvider.getContainersByIds(widget.assignedContainers); + final equipmentIds = + widget.assignedEquipment.map((eq) => eq.equipmentId).toList(); + final containers = + await containerProvider.getContainersByIds(widget.assignedContainers); final childEquipmentIds = []; for (final container in containers) { childEquipmentIds.addAll(container.equipmentIds); } - final allEquipmentIds = {...equipmentIds, ...childEquipmentIds}.toList(); - final equipment = await equipmentProvider.getEquipmentsByIds(allEquipmentIds); + final allEquipmentIds = + {...equipmentIds, ...childEquipmentIds}.toList(); + final equipment = + await equipmentProvider.getEquipmentsByIds(allEquipmentIds); _equipmentCache.clear(); _containerCache.clear(); @@ -110,7 +121,9 @@ class _EventAssignedEquipmentSectionState extends State _isLoading = false); } @@ -138,7 +151,8 @@ class _EventAssignedEquipmentSectionState extends State _processSelection(Map selection) async { - DebugLog.info('[EventAssignedEquipmentSection] Processing selection of ${selection.length} items'); + DebugLog.info( + '[EventAssignedEquipmentSection] Processing selection of ${selection.length} items'); // Séparer équipements et conteneurs final newEquipment = []; @@ -155,23 +169,27 @@ class _EventAssignedEquipmentSectionState extends State(); - final containers = await containerProvider.getContainersByIds(newContainers); + final containers = + await containerProvider.getContainersByIds(newContainers); for (var container in containers) { for (var childEquipmentId in container.equipmentIds) { // Vérifier si l'équipement enfant n'est pas déjà dans la liste - final existsInNew = newEquipment.any((eq) => eq.equipmentId == childEquipmentId); + final existsInNew = + newEquipment.any((eq) => eq.equipmentId == childEquipmentId); if (!existsInNew) { newEquipment.add(EventEquipment( equipmentId: childEquipmentId, quantity: 1, )); - DebugLog.info('[EventAssignedEquipmentSection] Adding child equipment $childEquipmentId from container ${container.id}'); + DebugLog.info( + '[EventAssignedEquipmentSection] Adding child equipment $childEquipmentId from container ${container.id}'); } } } @@ -183,11 +201,12 @@ class _EventAssignedEquipmentSectionState extends State e.equipmentId == eq.equipmentId); - + final existingIndex = + updatedEquipment.indexWhere((e) => e.equipmentId == eq.equipmentId); + if (existingIndex != -1) { // L'équipement existe déjà : mettre à jour la quantité updatedEquipment[existingIndex] = EventEquipment( @@ -204,17 +223,85 @@ class _EventAssignedEquipmentSectionState extends State _openAiAssistantDialog() async { + if (widget.startDate == null || widget.endDate == null) { + return; + } + + final result = await showDialog( + context: context, + builder: (context) => AiEquipmentAssistantDialog( + startDate: widget.startDate!, + endDate: widget.endDate!, + eventTypeId: widget.eventTypeId, + excludeEventId: widget.eventId, + currentAssignedEquipment: widget.assignedEquipment, + ), + ); + + if (result == null) { + return; + } + + _applyAiProposal(result); + } + + void _applyAiProposal(AiProposalResult result) async { + final existingById = { + for (final equipment in widget.assignedEquipment) + equipment.equipmentId: equipment, + }; + + final updatedEquipment = result.equipment.map((proposed) { + final existing = existingById[proposed.equipmentId]; + if (existing == null) { + return proposed; + } + return existing.copyWith(quantity: proposed.quantity, rationale: proposed.rationale); + }).toList(); + + // 🔧 FIX: Pour chaque container ajouté par l'IA, ajouter aussi ses équipements enfants + if (result.containerIds.isNotEmpty) { + final containerProvider = context.read(); + final containers = await containerProvider.getContainersByIds(result.containerIds); + + for (var container in containers) { + for (var childEquipmentId in container.equipmentIds) { + // Vérifier si l'équipement enfant n'est pas déjà dans la liste (ou déjà ajouté par la proposition) + final exists = updatedEquipment.any((eq) => eq.equipmentId == childEquipmentId); + if (!exists) { + updatedEquipment.add(EventEquipment( + equipmentId: childEquipmentId, + quantity: 1, + rationale: 'Inclus dans ${container.id}', + )); + DebugLog.info('[EventAssignedEquipmentSection] AI adding child equipment $childEquipmentId from container ${container.id}'); + } + } + } + } + + final updatedContainers = [...widget.assignedContainers]; + for (final containerId in result.containerIds) { + if (!updatedContainers.contains(containerId)) { + updatedContainers.add(containerId); + } + } + + widget.onChanged(updatedEquipment, updatedContainers); + } + void _removeEquipment(String equipmentId) { final updated = widget.assignedEquipment .where((eq) => eq.equipmentId != equipmentId) @@ -231,9 +318,8 @@ class _EventAssignedEquipmentSectionState extends State id != containerId) - .toList(); + final updatedContainers = + widget.assignedContainers.where((id) => id != containerId).toList(); // 🔧 FIX: Ne supprimer les équipements enfants QUE s'ils ne sont pas dans un autre container final updatedEquipment = []; @@ -252,8 +338,10 @@ class _EventAssignedEquipmentSectionState extends State eq.equipmentId).toSet(); + final remainingEquipmentIds = + updatedEquipment.map((eq) => eq.equipmentId).toSet(); for (var equipmentId in container.equipmentIds) { if (!remainingEquipmentIds.contains(equipmentId)) { _equipmentCache.remove(equipmentId); @@ -301,7 +390,8 @@ class _EventAssignedEquipmentSectionState extends State