import { useState, useEffect } from 'react'; import { supabase } from '@/integrations/supabase/client'; import { ComboboxOption } from '@/components/ui/combobox'; export function useCountries() { const [countries, setCountries] = useState([]); const [loading, setLoading] = useState(false); useEffect(() => { async function fetchCountries() { setLoading(true); try { const { data, error } = await supabase .from('locations') .select('country') .not('country', 'is', null); if (error) throw error; const uniqueCountries = Array.from( new Set(data?.map(item => item.country) || []) ).sort(); setCountries( uniqueCountries.map(country => ({ label: country, value: country.toLowerCase().replace(/\s+/g, '_') })) ); } catch (error) { console.error('Error fetching countries:', error); setCountries([]); } finally { setLoading(false); } } fetchCountries(); }, []); return { countries, loading }; } export function useStatesProvinces(country?: string) { const [statesProvinces, setStatesProvinces] = useState([]); const [loading, setLoading] = useState(false); useEffect(() => { if (!country) { setStatesProvinces([]); return; } async function fetchStatesProvinces() { setLoading(true); try { const { data, error } = await supabase .from('locations') .select('state_province') .eq('country', country) .not('state_province', 'is', null); if (error) throw error; const uniqueStates = Array.from( new Set(data?.map(item => item.state_province) || []) ).sort(); setStatesProvinces( uniqueStates.map(state => ({ label: state, value: state.toLowerCase().replace(/\s+/g, '_') })) ); } catch (error) { console.error('Error fetching states/provinces:', error); setStatesProvinces([]); } finally { setLoading(false); } } fetchStatesProvinces(); }, [country]); return { statesProvinces, loading }; } export function useManufacturers() { const [manufacturers, setManufacturers] = useState([]); const [loading, setLoading] = useState(false); useEffect(() => { async function fetchManufacturers() { setLoading(true); try { const { data, error } = await supabase .from('companies') .select('id, name') .eq('company_type', 'manufacturer') .order('name'); if (error) throw error; setManufacturers( (data || []).map(company => ({ label: company.name, value: company.id })) ); } catch (error) { console.error('Error fetching manufacturers:', error); setManufacturers([]); } finally { setLoading(false); } } fetchManufacturers(); }, []); return { manufacturers, loading }; } export function useRideModels(manufacturerId?: string) { const [rideModels, setRideModels] = useState([]); const [loading, setLoading] = useState(false); useEffect(() => { if (!manufacturerId) { setRideModels([]); return; } async function fetchRideModels() { setLoading(true); try { const { data, error } = await supabase .from('ride_models') .select('id, name') .eq('manufacturer_id', manufacturerId) .order('name'); if (error) throw error; setRideModels( (data || []).map(model => ({ label: model.name, value: model.id })) ); } catch (error) { console.error('Error fetching ride models:', error); setRideModels([]); } finally { setLoading(false); } } fetchRideModels(); }, [manufacturerId]); return { rideModels, loading }; } export function useCompanyHeadquarters() { const [headquarters, setHeadquarters] = useState([]); const [loading, setLoading] = useState(false); useEffect(() => { async function fetchHeadquarters() { setLoading(true); try { const { data, error } = await supabase .from('companies') .select('headquarters_location') .not('headquarters_location', 'is', null); if (error) throw error; const uniqueHeadquarters = Array.from( new Set(data?.map(item => item.headquarters_location) || []) ).sort(); setHeadquarters( uniqueHeadquarters.map(hq => ({ label: hq, value: hq.toLowerCase().replace(/\s+/g, '_') })) ); } catch (error) { console.error('Error fetching headquarters:', error); setHeadquarters([]); } finally { setLoading(false); } } fetchHeadquarters(); }, []); return { headquarters, loading }; } export function useOperators() { const [operators, setOperators] = useState([]); const [loading, setLoading] = useState(false); useEffect(() => { async function fetchOperators() { setLoading(true); try { const { data, error } = await supabase .from('companies') .select('id, name') .eq('company_type', 'operator') .order('name'); if (error) throw error; setOperators( (data || []).map(company => ({ label: company.name, value: company.id })) ); } catch (error) { console.error('Error fetching operators:', error); setOperators([]); } finally { setLoading(false); } } fetchOperators(); }, []); return { operators, loading }; } export function usePropertyOwners() { const [propertyOwners, setPropertyOwners] = useState([]); const [loading, setLoading] = useState(false); useEffect(() => { async function fetchPropertyOwners() { setLoading(true); try { const { data, error } = await supabase .from('companies') .select('id, name') .eq('company_type', 'property_owner') .order('name'); if (error) throw error; setPropertyOwners( (data || []).map(company => ({ label: company.name, value: company.id })) ); } catch (error) { console.error('Error fetching property owners:', error); setPropertyOwners([]); } finally { setLoading(false); } } fetchPropertyOwners(); }, []); return { propertyOwners, loading }; }