// Package app is the composition root for the running administration service.
// It knows which business modules are enabled and wires their contributions
// into the shared platform. Individual modules do not import this package.
package app

import (
	integrationmodule "kra/internal/modules/integration"
	paymentmodule "kra/internal/modules/payment"
	systemmodule "kra/internal/modules/system"
	taskmodule "kra/internal/modules/task"
	"kra/pkg/module"
	platformtask "kra/pkg/task"
)

// Catalog lists the business modules enabled in this binary. Static module
// contributions (migrations, admin metadata, default tasks) enter through a
// Definition; runtime routes and dependency-bearing task contributors still
// need explicit wiring below.
func Catalog() module.Catalog {
	return module.Catalog{Definitions: []module.Definition{
		systemmodule.Definition(),
		integrationmodule.Definition(),
		taskmodule.Definition(),
		paymentmodule.Definition(),
	}}
}

// TaskRegistry builds the process-wide registry from dependency-free module
// contributions. Dependency-bearing methods are added by their module runtime
// constructors after the usecases have been created.
func TaskRegistry(catalog module.Catalog) *platformtask.Registry {
	registry := platformtask.NewRegistry()
	registry.RegisterAll(catalog.TaskMethods())
	return registry
}

// RuntimeContributions groups runtime objects that need constructed
// dependencies. The binary composition root supplies the concrete modules.
type RuntimeContributions struct {
	Routes []module.RouteRegistrar
	Tasks  []platformtask.Contributor
}

// Runtime activates dependency-bearing tasks and composes module routes.
func Runtime(contributions RuntimeContributions, registry *platformtask.Registry) *module.Runtime {
	platformtask.Apply(registry, contributions.Tasks...)
	return module.NewRuntime(contributions.Routes...)
}
