from __future__ import annotations

from clarabot_nano.common.config.extract.admin_password import AdminPasswordExtractedConfig
from clarabot_nano.common.config.extract.data_dir import DataDirExtractedConfig
from clarabot_nano.common.config.extract.path_normalization import PathUnicodeNormalizationExtractedConfig
from clarabot_nano.common.config.extract.secrets import SecretsExtractedConfig
from clarabot_nano.common.config.extract.solr import SolrExtractedConfig
from clarabot_nano.common.solr.rest import create_nano_query_url, create_solr_auth_header
from clarabot_nano.utility.serialize import msgpack_loads


class ServerConfig(
    AdminPasswordExtractedConfig,
    DataDirExtractedConfig,
    SolrExtractedConfig,
    SecretsExtractedConfig,
    PathUnicodeNormalizationExtractedConfig,
):
    """
    Configurations relevant to the job of the server are extracted from the core-config here.
    """

    def __init__(self, config):
        AdminPasswordExtractedConfig.__init__(self, config)
        DataDirExtractedConfig.__init__(self, config)
        SolrExtractedConfig.__init__(self, config)
        SecretsExtractedConfig.__init__(self, config)
        PathUnicodeNormalizationExtractedConfig.__init__(self, config)
        self.name = config["remote"]["name"]
        self.remote_files_restriction = config["remote"]["remote_files_restriction"]
        self.restrict_sensitive_path_access = config["local"]["restrict_sensitive_path_access"]
        # Create a safe copy of the remote config to be ready for query by the owner.
        self.remote_config = dict(config["remote"])
        self.remote_config.pop("secrets", None)
        self.remote_config.pop("admin_password", None)
        self.gateway_port = config["state"].get("gateway_port")  # int if present
        self.gateway_key = config["state"].get("gateway_key")
        if self.gateway_key:
            self.gateway_key = msgpack_loads(self.gateway_key)
        # prepare these from the extracted config
        self.solr_query_url = (
            create_nano_query_url(self.solr_port, self.solr_core) if self.solr_port and self.solr_core else None
        )
        self.solr_auth_header = create_solr_auth_header(self.solr_key) if self.solr_key else ""

    def _matches(self, other: ServerConfig) -> bool:
        return (
            AdminPasswordExtractedConfig._matches(self, other)
            and DataDirExtractedConfig._matches(self, other)
            and SolrExtractedConfig._matches(self, other)
            and SecretsExtractedConfig._matches(self, other)
            and PathUnicodeNormalizationExtractedConfig._matches(self, other)
            and self.name == other.name
            and self.remote_files_restriction == other.remote_files_restriction
            and self.restrict_sensitive_path_access == other.restrict_sensitive_path_access
            and self.remote_config == other.remote_config
            and self.gateway_port == other.gateway_port
            and self.gateway_key == other.gateway_key
        )
