package plugins

import (
	"context"
	"errors"
	"testing"
)

type mockLicenseChecker struct{ err error }

func (m *mockLicenseChecker) CheckFeature(_ string) error { return m.err }

func TestServiceCallBlockedWithoutLicense(t *testing.T) {
	s := NewService(nil, 0, nil).WithLicense(&mockLicenseChecker{err: errors.New("license missing")})
	err := s.Call(context.Background(), "test-plugin", "sms", "sms.send", nil, nil)
	if err == nil {
		t.Fatal("expected error without valid license")
	}
	if !errors.Is(err, ErrLicenseRequired) {
		t.Fatalf("expected ErrLicenseRequired, got: %v", err)
	}
}

func TestServiceCallAllowedWithLicense(t *testing.T) {
	// With a valid license checker (no error), it should proceed past the
	// license check and fail only because registry is nil (ErrNotFound).
	s := NewService(nil, 0, nil).WithLicense(&mockLicenseChecker{err: nil})
	err := s.Call(context.Background(), "test-plugin", "sms", "sms.send", nil, nil)
	if errors.Is(err, ErrLicenseRequired) {
		t.Fatal("should not get ErrLicenseRequired with valid license")
	}
	// ErrNotFound is expected since registry is nil
	if !errors.Is(err, ErrNotFound) {
		t.Fatalf("expected ErrNotFound (nil registry), got: %v", err)
	}
}

func TestFeaturePluginsConstantMatch(t *testing.T) {
	// FeaturePlugins must equal "plugins" to match license.FeaturePlugins.
	if FeaturePlugins != "plugins" {
		t.Fatalf("FeaturePlugins = %q, want %q", FeaturePlugins, "plugins")
	}
}
