// observations_endpoint.go defines the /observations endpoint behavior.
// Layer: adapters/inbound/httpapi observation route.
package httpapi

import (
	"context"

	"gitea.maximumdirect.net/ejr/feedapi/endpoint"
	"gitea.maximumdirect.net/ejr/feedapi/render"
	"gitea.maximumdirect.net/ejr/feedapi/response"
	"gitea.maximumdirect.net/ejr/weatherapi/internal/adapters/inbound/httpapi/presenter"
)

func observationDefinition(svc Service) endpoint.Definition {
	return endpoint.GET(
		"/observations",
		bindPrecisionQuery,
		func(ctx context.Context, req precisionQueryRequest) (any, error) {
			obs, err := svc.LatestObservation(ctx)
			if err != nil {
				return nil, err
			}
			return response.Envelope{Data: presenter.ObservationPayload(obs, req.Units, req.Precision)}, nil
		},
		endpoint.WithProduces(render.FormatJSON, render.FormatXML, render.FormatText),
		endpoint.WithTemplate("observations.txt.tmpl"),
	)
}
