import supabase from '../supabaseClient'; function applyRegionFilter(query, regionId) { if (!regionId) return query; return query.eq('region_id', regionId); } export async function getAllInfosSillons(regionId = null) { try { const query = applyRegionFilter( supabase.from('infos_sillons').select('*').order('created_at', { ascending: false }), regionId ); const { data, error } = await query; if (error) { console.error('[API InfosSillons] getAllInfosSillons error:', error); return { data: null, error }; } return { data: data || [], error: null }; } catch (err) { console.error('[API InfosSillons] getAllInfosSillons exception:', err); return { data: null, error: { message: 'Erreur lors du chargement des infos sillons' } }; } } export async function createInfoSillon(infoData) { const sanitized = { titre: infoData.titre?.trim() || '', description: infoData.description || null, type: infoData.type || 'information', ligne_id: infoData.ligne_id || null, ligne_nom: infoData.ligne_nom || null, travaux_id: infoData.travaux_id || null, date_debut: infoData.date_debut || null, date_fin: infoData.date_fin || null, actif: infoData.actif !== false, auto_generated: infoData.auto_generated === true, region_id: infoData.region_id || null }; if (!sanitized.titre) { return { data: null, error: { message: 'Le titre est requis' } }; } try { const { data, error } = await supabase .from('infos_sillons') .insert(sanitized) .select() .single(); if (error) { console.error('[API InfosSillons] createInfoSillon error:', error); return { data: null, error }; } return { data, error: null }; } catch (err) { console.error('[API InfosSillons] createInfoSillon exception:', err); return { data: null, error: { message: 'Erreur lors de la création' } }; } } export async function updateInfoSillon(id, infoData) { if (!id) return { data: null, error: { message: 'ID manquant' } }; const sanitized = { titre: infoData.titre?.trim() || '', description: infoData.description || null, type: infoData.type || 'information', ligne_id: infoData.ligne_id || null, ligne_nom: infoData.ligne_nom || null, travaux_id: infoData.travaux_id || null, date_debut: infoData.date_debut || null, date_fin: infoData.date_fin || null, actif: infoData.actif !== false, auto_generated: infoData.auto_generated === true, region_id: infoData.region_id || null }; if (!sanitized.titre) { return { data: null, error: { message: 'Le titre est requis' } }; } try { const { data, error } = await supabase .from('infos_sillons') .update(sanitized) .eq('id', id) .select() .single(); if (error) { console.error('[API InfosSillons] updateInfoSillon error:', error); return { data: null, error }; } return { data, error: null }; } catch (err) { console.error('[API InfosSillons] updateInfoSillon exception:', err); return { data: null, error: { message: 'Erreur lors de la mise à jour' } }; } } export async function deleteInfoSillon(id) { if (!id) return { data: null, error: { message: 'ID manquant' } }; try { const { data, error } = await supabase .from('infos_sillons') .delete() .eq('id', id) .select('id') .maybeSingle(); if (error) { console.error('[API InfosSillons] deleteInfoSillon error:', error); return { data: null, error }; } if (!data?.id) { return { data: null, error: { message: 'Suppression impossible: élément introuvable.' } }; } return { data: { id: data.id, deleted: true }, error: null }; } catch (err) { console.error('[API InfosSillons] deleteInfoSillon exception:', err); return { data: null, error: { message: 'Erreur lors de la suppression' } }; } }