package jobs

import (
	"errors"
	"fmt"

	licenseclient "qycw/go-server/internal/license"
)

var ErrCommercialLicenseRequired = errors.New("commercial license required for worker execution")

type workerLicenseGate struct {
	eval *licenseclient.Evaluator
}

func newWorkerLicenseGate(eval *licenseclient.Evaluator) workerLicenseGate {
	return workerLicenseGate{eval: eval}
}

func (g workerLicenseGate) check() error {
	return g.checkFeature(licenseclient.FeatureWorker)
}

func (g workerLicenseGate) checkFeature(feature string) error {
	if g.eval == nil {
		return fmt.Errorf("%w: %w", ErrCommercialLicenseRequired, licenseclient.ErrLicenseMissing)
	}
	if err := g.eval.CheckFeature(feature); err != nil {
		return fmt.Errorf("%w: %w", ErrCommercialLicenseRequired, err)
	}
	return nil
}
