package server

import (
	"net/http"

	"github.com/labstack/echo/v4"

	"git.maze.io/ham/hamview/schema"
)

// handleGetRadios returns a list of radio stations/receivers.
//
// Endpoint: GET /api/v1/radios
// Endpoint: GET /api/v1/radios/:protocol
//
// Path Parameters:
//   - protocol (optional): Filter by protocol name (e.g., "meshcore", "aprs")
//
// Response: 200 OK
//
//	[]Radio - Array of radio objects
//
// Response: 500 Internal Server Error
//
//	ErrorResponse - Error retrieving radios
//
// Example Request:
//
//	GET /api/v1/radios
//	GET /api/v1/radios/meshcore
//
// Example Response:
//
//	[
//	  {
//	    "id": 1,
//	    "name": "Station-Alpha",
//	    "is_online": true,
//	    "manufacturer": "Heltec",
//	    "device": "WiFi LoRa 32 V3",
//	    "modulation": "LoRa",
//	    "protocol": "meshcore",
//	    "latitude": 52.3667,
//	    "longitude": 4.8945,
//	    "frequency": 868.1,
//	    "bandwidth": 125.0,
//	    "created_at": "2026-01-01T12:00:00Z",
//	    "updated_at": "2026-03-05T10:30:00Z"
//	  }
//	]
func (s *Server) handleGetRadios(c echo.Context) error {
	var (
		radios []*schema.Radio
		err    error
	)

	if protocol := c.Param("protocol"); protocol != "" {
		radios, err = schema.GetRadiosByProtocol(c.Request().Context(), protocol)
	} else {
		radios, err = schema.GetRadios(c.Request().Context())
	}

	if err != nil {
		return s.apiError(c, err)
	}

	return c.JSON(http.StatusOK, radios)
}
