package api

import (
	"net/http"
	"strings"
)

// ParseActionSegment splits a route segment shaped like "resource-id:action".
// It exists because Gin cannot register a single path segment containing both a
// path parameter and a literal :action suffix (for example :id:pause). Route
// registrations should capture the full segment and call this helper before
// dispatching the action.
func ParseActionSegment(raw string, idField string) (string, string, error) {
	raw = strings.TrimSpace(raw)
	separator := strings.LastIndex(raw, ":")
	if separator < 0 {
		return "", "", NewHTTPError(http.StatusBadRequest, "action is required")
	}
	id := strings.TrimSpace(raw[:separator])
	if id == "" {
		return "", "", NewHTTPError(http.StatusBadRequest, idField+" is required")
	}
	action := strings.TrimSpace(raw[separator+1:])
	if action == "" {
		return "", "", NewHTTPError(http.StatusBadRequest, "action is required")
	}
	return id, action, nil
}
