package controllers

import (
	"log"
	"net/http"

	"github.com/gin-gonic/gin"
	"gitlab.com/pactual1/backend/config"
	"gitlab.com/pactual1/backend/services/location"
)

func SearchPlace(c *gin.Context) {
	query := c.Query("q")

	if query == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "query is empty"})
		return
	}

	mapboxAccessToken := config.AppConfig.Service.MapboxAccessToken
	locationClient := location.NewService(mapboxAccessToken)

	places, err := locationClient.SearchPlace(c, query)
	if err != nil {
		log.Printf("SearchPlace Error: Service error: %v", err)
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Service error"})
		return
	}

	// Respond with the buyers and the total count
	c.JSON(http.StatusOK, gin.H{"data": places})
}
