// alerts_endpoint.go defines the /alerts/active endpoint behavior.
// Layer: adapters/inbound/httpapi alerts 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 alertsDefinition(svc Service) endpoint.Definition {
	return endpoint.GET(
		"/alerts/active",
		bindQuery,
		func(ctx context.Context, req queryRequest) (any, error) {
			run, err := svc.LatestAlertRun(ctx)
			if err != nil {
				return nil, err
			}
			return response.Envelope{Data: presenter.AlertsPayload(run, req.Units)}, nil
		},
		endpoint.WithProduces(render.FormatJSON, render.FormatXML, render.FormatText),
		endpoint.WithTemplate("alerts_active.txt.tmpl"),
	)
}
