//go:build !lite

package api

import (
	"net/http"

	"breadbox/internal/service"
)

// TriggerSyncHandler triggers a manual sync of all connections, or a single connection if connection_id is provided.
func TriggerSyncHandler(svc *service.Service) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var connectionID *string

		if r.Body != nil && r.ContentLength != 0 {
			var body struct {
				ConnectionID *string `json:"connection_id"`
			}
			if !decodeJSON(w, r, &body) {
				return
			}
			connectionID = body.ConnectionID
		}

		if err := svc.TriggerSync(r.Context(), connectionID); err != nil {
			writeServiceError(w, err, "Connection not found", "Failed to trigger sync")
			return
		}

		writeJSON(w, http.StatusAccepted, map[string]string{"status": "sync_triggered"})
	}
}
