from clarabot_nano.common.crypto.admin_password import ADMIN_PASSWORD_CONTEXT
from clarabot_nano.core.config.update.base import AbstractRemoteConfigUpdateHandler


class UpdateAdminPassword(AbstractRemoteConfigUpdateHandler):
    HANDLER_KEY = "admin_password"

    def _execute(self, data: dict):
        pw = data.get("admin_password")
        if not isinstance(pw, str):
            raise Exception("Admin password must be string")
        elif not pw:
            raise Exception("Admin password can't be empty")
        self._c["admin_password"] = ADMIN_PASSWORD_CONTEXT.hash(pw)

    def _describe_change(self, old_config: dict, new_config: dict, data: dict) -> str:
        return "Nano admin password changed"  # logging it would be a security risk
