package mapbox

import (
	"encoding/json"
	"fmt"
	"net/http"
	"time"
)

type Client struct {
	AccessToken string
	HTTPClient  *http.Client
}

func New(token string) *Client {
	return &Client{
		AccessToken: token,
		HTTPClient: &http.Client{
			Timeout: 10 * time.Second,
		},
	}
}

type directionsResponse struct {
	Routes []struct {
		Distance float64 `json:"distance"` // meters
		Duration float64 `json:"duration"` // seconds
	} `json:"routes"`
	Code string `json:"code"`
}

// GetDrivingDistance returns distance in kilometers between two points
func (c *Client) GetDrivingDistance(lat1, lon1, lat2, lon2 float64) (float64, error) {
	// Mapbox Directions API: /driving/{lon},{lat};{lon},{lat}
	url := fmt.Sprintf("https://api.mapbox.com/directions/v5/mapbox/driving/%f,%f;%f,%f?access_token=%s",
		lon1, lat1, lon2, lat2, c.AccessToken)

	resp, err := c.HTTPClient.Get(url)
	if err != nil {
		return 0, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return 0, fmt.Errorf("mapbox api error: status %d", resp.StatusCode)
	}

	var result directionsResponse
	if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
		return 0, err
	}

	if len(result.Routes) == 0 {
		return 0, fmt.Errorf("no route found")
	}

	// Convert meters to km
	return result.Routes[0].Distance / 1000.0, nil
}
