package workers

import (
	"context"

	"go.uber.org/zap"
)

type Worker interface {
	Name() string
	Start(ctx context.Context) error
}

type Manager struct {
	workers []Worker
	log     *zap.Logger
}

func NewManager(log *zap.Logger, workers ...Worker) *Manager {
	return &Manager{workers: workers, log: log}
}

func (m *Manager) Start(ctx context.Context) {
	for _, worker := range m.workers {
		worker := worker
		go func() {
			m.log.Info("starting worker", zap.String("worker", worker.Name()))
			if err := worker.Start(ctx); err != nil {
				m.log.Error("worker stopped", zap.String("worker", worker.Name()), zap.Error(err))
			}
		}()
	}
}
