import { ref } from 'vue'
import api from '../axios.js'

export function usePrediction() {
    const prediction = ref(null)
    const loading = ref(false)
    const error = ref(null)

    async function fetch({ lat, lng } = {}) {
        loading.value = true
        error.value = null
        prediction.value = null

        const params = {}
        if (lat && lng) {
            params.lat = lat
            params.lng = lng
        }

        try {
            const response = await api.get('/prediction', { params })
            prediction.value = response.data
        } catch (err) {
            error.value = 'Unable to load prediction.'
        } finally {
            loading.value = false
        }
    }

    return { prediction, loading, error, fetch }
}
