package daemon

import (
	"context"

	"banger/internal/api"
)

// ListOperations returns a snapshot of every async operation tracked
// across the daemon's per-kind registries. Today the only kind is
// vm.create; future async kinds (image build, kernel pull) will plug
// in here.
//
// The primary consumer is `banger update`'s preflight, which refuses
// to swap binaries while anything is in flight. Done operations are
// included in the snapshot so an operator running an interactive
// `banger ... | jq` can see recently-completed work; the update
// preflight filters by Done itself.
func (d *Daemon) ListOperations(_ context.Context) (api.OperationsListResult, error) {
	out := api.OperationsListResult{Operations: []api.OperationSummary{}}
	if d.vm == nil {
		return out, nil
	}
	for _, op := range d.vm.createOps.List() {
		snap := op.snapshot()
		out.Operations = append(out.Operations, api.OperationSummary{
			ID:        snap.ID,
			Kind:      "vm.create",
			Stage:     snap.Stage,
			Detail:    snap.Detail,
			Done:      snap.Done,
			StartedAt: snap.StartedAt,
			UpdatedAt: snap.UpdatedAt,
		})
	}
	return out, nil
}
