from dataclasses import dataclass
from os import getenv
from pathlib import Path

from csirtmu.core.dataclass_config_loader import load_dataclass_config

BASE_DIR = Path(__file__).parent.parent.parent
ENV_PREFIX = "CSIRTMU_SELF_SERVICE_FRONTEND"


@dataclass
class BackendConfig:
    host: str
    port: int

    @property
    def first_step_url(self) -> str:
        return f"{self.host}:{self.port}/steps/first"

    @property
    def next_step_url(self) -> str:
        return f"{self.host}:{self.port}/steps/next"

    @property
    def add_step_url(self) -> str:
        return f"{self.host}:{self.port}/steps/add"

    @property
    def get_tickets_url(self) -> str:
        return f"{self.host}:{self.port}/tickets"

    @property
    def submit_form_url(self) -> str:
        return f"{self.host}:{self.port}/submit"

    @property
    def netbox_search_url(self) -> str:
        return f"{self.host}:{self.port}/netbox"

    @property
    def outline_search_url(self) -> str:
        return f"{self.host}:{self.port}/outline"


@dataclass(unsafe_hash=True)
class Config:
    backend: BackendConfig


class AppConfig:
    _config: Config | None = None

    @classmethod
    def get(cls, config_path: Path | None = None) -> Config:
        if cls._config is None:
            if config_path is None:  # pragma: no cover
                config_path = Path(
                    getenv(
                        "CSIRTMU_SELF_SERVICE_FRONTEND_CONFIG_PATH",
                        (BASE_DIR / "config/config.yaml").resolve(),
                    )
                )
            cls._config = load_dataclass_config(config_path, Config, ENV_PREFIX)
        return cls._config
