package executor

import (
	"context"
	"fmt"

	"scheduler-backend/internal/jobdef"
)

type Service struct {
	registry *jobdef.Registry
	runtime  jobdef.Runtime
}

func NewService(registry *jobdef.Registry, runtime jobdef.Runtime) *Service {
	return &Service{
		registry: registry,
		runtime:  runtime,
	}
}

func (s *Service) Execute(ctx context.Context, handlerKey string, req jobdef.ExecuteRequest) error {
	handler, ok := s.registry.Get(handlerKey)
	if !ok {
		return fmt.Errorf("handler %s not found", handlerKey)
	}

	if req.LogCollector == nil {
		req.LogCollector = jobdef.NewLogCollector()
	}

	return handler.Run(ctx, s.runtime, req)
}
