const HUB = location.hostname === 'localhost' ? 'http://localhost:3300' : 'https://msg.gigafibre.ca'

const get = async (path) => { const r = await fetch(HUB + path); if (!r.ok) throw new Error(`Hub ${r.status}`); return r.json() }
const post = async (path, body) => { const r = await fetch(HUB + path, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }); if (!r.ok) throw new Error(`Hub ${r.status}`); return r.json() }

export const fetchCatalog = () => get('/api/catalog')
export const submitOrder = (payload) => post('/api/checkout', payload)
export const getOrderStatus = (id) => get(`/api/order/${encodeURIComponent(id)}`)
export const searchAddresses = async (q, limit = 8) => (await post('/api/address-search', { q, limit })).results || []
export const sendOTP = (identifier) => post('/api/otp/send', { identifier })
export const verifyOTP = (identifier, code) => post('/api/otp/verify', { identifier, code })
