import { SUPABASE_URL } from '@/supabase'

const STORAGE_URL = `${SUPABASE_URL}/storage/v1/object/public/uploads/`

/**
 * Returns a full URL for an image path.
 * Handles Supabase Storage paths and absolute URLs (legacy or external).
 */
export function getImageUrl(path?: string | null, type: 'taxi' | 'shuttle' | 'business' | 'coupon' = 'business') {
    if (!path) {
        const defaultNames: Record<string, string> = {
            taxi: 'Taxi',
            shuttle: 'Transporte',
            business: 'Negocio',
            coupon: 'Oferta'
        }
        const name = defaultNames[type] || 'SIB'
        return `https://ui-avatars.com/api/?name=${name}&background=fee715&color=101820&size=256&bold=true`
    }

    // Already a full URL (Supabase Storage or external)
    if (path.startsWith('http')) {
        return path
    }

    // Relative path → Supabase Storage
    const cleanPath = path.startsWith('/') ? path.slice(1) : path
    return `${STORAGE_URL}${cleanPath}`
}
