// Copyright (c) 2026 Sidero Labs, Inc.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.

package omni

import (
	"github.com/cosi-project/runtime/pkg/controller/generic/cleanup"

	"github.com/siderolabs/omni/client/pkg/omni/resources/omni"
	customcleanup "github.com/siderolabs/omni/internal/backend/runtime/omni/controllers/cleanup"
)

// MachineCleanupController manages MachineCleanup resource lifecycle.
type MachineCleanupController = cleanup.Controller[*omni.Machine]

// NewMachineCleanupController returns a new MachineCleanup controller.
// This controller should remove all MachineSetNodes for a tearing down machine.
// If the MachineSetNode is owned by some controller, it is skipped.
func NewMachineCleanupController() *MachineCleanupController {
	return cleanup.NewController(
		cleanup.Settings[*omni.Machine]{
			Name: "MachineCleanupController",
			Handler: cleanup.Combine(
				&customcleanup.SameIDHandler[*omni.Machine, *omni.MachineSetNode]{},
				&customcleanup.SameIDHandler[*omni.Machine, *omni.InfraMachineConfig]{},
				&customcleanup.SameIDHandler[*omni.Machine, *omni.InfraMachineBMCConfig]{},
				&customcleanup.SameIDHandler[*omni.Machine, *omni.KernelArgs]{},
			),
		},
	)
}
