"""
Tests for plugin registration and the secrets_manager_plugin object.
"""

import pytest
from unittest.mock import MagicMock

from elizaos_plugin_secrets_manager.plugin import secrets_manager_plugin


class TestPluginRegistration:
    def test_plugin_name(self):
        assert secrets_manager_plugin.name == "secrets-manager"

    def test_plugin_has_description(self):
        assert secrets_manager_plugin.description
        assert "secret" in secrets_manager_plugin.description.lower()

    def test_plugin_has_services(self):
        assert secrets_manager_plugin.services is not None
        assert len(secrets_manager_plugin.services) >= 2

    def test_plugin_has_actions(self):
        assert secrets_manager_plugin.actions is not None
        assert len(secrets_manager_plugin.actions) >= 3

    def test_plugin_has_providers(self):
        assert secrets_manager_plugin.providers is not None
        assert len(secrets_manager_plugin.providers) >= 4

    def test_actions_include_set_secret(self):
        action_names = [getattr(a, "name", "") for a in secrets_manager_plugin.actions]
        assert "SET_SECRET" in action_names

    def test_actions_include_manage_secret(self):
        action_names = [getattr(a, "name", "") for a in secrets_manager_plugin.actions]
        assert "MANAGE_SECRET" in action_names

    def test_actions_include_update_settings(self):
        action_names = [getattr(a, "name", "") for a in secrets_manager_plugin.actions]
        assert "UPDATE_SETTINGS" in action_names

    def test_providers_include_secrets_status(self):
        provider_names = [getattr(p, "name", "") for p in secrets_manager_plugin.providers]
        assert "SECRETS_STATUS" in provider_names

    def test_providers_include_secrets_info(self):
        provider_names = [getattr(p, "name", "") for p in secrets_manager_plugin.providers]
        assert "SECRETS_INFO" in provider_names

    def test_providers_include_onboarding_settings(self):
        provider_names = [getattr(p, "name", "") for p in secrets_manager_plugin.providers]
        assert "ONBOARDING_SETTINGS" in provider_names

    def test_providers_include_missing_secrets(self):
        provider_names = [getattr(p, "name", "") for p in secrets_manager_plugin.providers]
        assert "MISSING_SECRETS" in provider_names


class TestModuleExports:
    def test_exports_secrets_service(self):
        from elizaos_plugin_secrets_manager import SecretsService
        assert SecretsService is not None

    def test_exports_plugin_activator(self):
        from elizaos_plugin_secrets_manager import PluginActivatorService
        assert PluginActivatorService is not None

    def test_exports_onboarding_service(self):
        from elizaos_plugin_secrets_manager import OnboardingService
        assert OnboardingService is not None

    def test_exports_storage_backends(self):
        from elizaos_plugin_secrets_manager import (
            MemorySecretStorage,
            CharacterSettingsStorage,
            WorldMetadataStorage,
            ComponentSecretStorage,
            CompositeSecretStorage,
        )
        assert MemorySecretStorage is not None
        assert CharacterSettingsStorage is not None
        assert WorldMetadataStorage is not None
        assert ComponentSecretStorage is not None
        assert CompositeSecretStorage is not None

    def test_exports_types(self):
        from elizaos_plugin_secrets_manager import (
            SecretLevel,
            SecretType,
            SecretStatus,
            StorageBackend,
            SecretPermissionType,
            SecretConfig,
            SecretContext,
            EncryptedSecret,
            SecretsError,
        )
        assert SecretLevel.GLOBAL.value == "global"
        assert SecretLevel.WORLD.value == "world"
        assert SecretLevel.USER.value == "user"

    def test_exports_crypto(self):
        from elizaos_plugin_secrets_manager import (
            KeyManager,
            generate_salt,
            generate_key,
            derive_key_from_agent_id,
            encrypt,
            decrypt,
            is_encrypted_secret,
        )
        assert callable(generate_salt)
        assert callable(generate_key)
        assert callable(encrypt)
        assert callable(decrypt)

    def test_exports_validation(self):
        from elizaos_plugin_secrets_manager import (
            validate_secret,
            register_validator,
            get_validator,
            infer_validation_strategy,
        )
        assert callable(validate_secret)

    def test_exports_onboarding_utilities(self):
        from elizaos_plugin_secrets_manager import (
            OnboardingSetting,
            OnboardingConfig,
            OnboardingFlowStep,
            OnboardingStepResult,
            create_onboarding_config,
            get_unconfigured_required,
            get_next_setting,
            is_onboarding_complete,
        )
        assert callable(create_onboarding_config)
        assert callable(is_onboarding_complete)
