package module

import (
	"context"
	"fmt"
	"time"
)

// Module is the interface every scanner module must implement.
type Module interface {
	// Name returns the unique identifier for this module (e.g. "arp_discovery").
	Name() string

	// Interval returns how often the scheduler should run this module.
	// A zero duration means the module runs once on startup only.
	Interval() time.Duration

	// Run executes one scan cycle. It must respect ctx cancellation.
	Run(ctx context.Context) error
}

// Registry holds all registered modules.
type Registry struct {
	modules []Module
}

// NewRegistry returns an empty module registry.
func NewRegistry() *Registry {
	return &Registry{}
}

// Register adds a module to the registry.
func (r *Registry) Register(m Module) {
	r.modules = append(r.modules, m)
}

// All returns all registered modules.
func (r *Registry) All() []Module {
	return r.modules
}

// Get returns the module with the given name.
func (r *Registry) Get(name string) (Module, error) {
	for _, m := range r.modules {
		if m.Name() == name {
			return m, nil
		}
	}
	return nil, fmt.Errorf("module %q not registered", name)
}
