package handlers

import (
	"fmt"
	"net/http"
	"time"

	"zlh-agent/internal/state"
)

func beginHandlerOperation(w http.ResponseWriter, opType string, maintenance bool, message string) (func(), bool) {
	end, ok, current := state.TryStartOperation(opType, maintenance, message)
	if ok {
		return end, true
	}
	msg := fmt.Sprintf("operation already in progress: %s", current.Type)
	if !current.StartedAt.IsZero() {
		msg = fmt.Sprintf("%s since %s", msg, current.StartedAt.UTC().Format(time.RFC3339))
	}
	writeJSONError(w, http.StatusConflict, msg)
	return nil, false
}
