from pydantic import BaseModel, field_validator
from remin_service.helper import DataCent
from typing import Optional
from remin_service.helper.ip_helper import get_local_ip, get_public_ip

# data = DataCent.data


class ImportConfig:

    def __init__(self, config_key):
        self._config_key = config_key

    def __call__(self, cls):
        _config_keys = self._config_key.split(".")
        # print(_config_keys)
        data = DataCent.data
        for _config_key in _config_keys:
            # print(data)
            data = data.get(_config_key, {})

        return cls(**data)


@ImportConfig("config")
class Config(BaseModel):
    active: str


@ImportConfig("service")
class ServiceConfig(BaseModel):
    ip: str = ""
    ip_type: str = "local"
    port: int = 8080
    service_name: str = "service"

    def __init__(self, **kwargs):
        if "ip" not in kwargs:
            kwargs["ip"] = "0.0.0.0"
            kwargs["ip_type"] = "public"
        elif kwargs.get("ip") == "0.0.0.0":
            kwargs["ip_type"] = "local_public"
        super().__init__(**kwargs)

    @classmethod
    def get_ip(cls):
        if ServiceConfig.ip_type == "local_public":
            return get_local_ip()
        if ServiceConfig.ip_type == "local":
            return "127.0.0.1"
        if ServiceConfig.ip_type == "public":
            return get_public_ip()


@ImportConfig("cloud.nacos")
class NaNosConfig(BaseModel):
    class __Discovery(BaseModel):
        username: str = None
        password: str = None
        cluster_name: str = ""
        namespace: str = ""
        group: str = ""
        server_addr: str

    discovery: Optional[__Discovery] = None





