from __future__ import annotations

from dataclasses import dataclass
from importlib.metadata import entry_points, PackageNotFoundError
from pathlib import Path
from typing import Iterable

PROGRAM_NAME = "curvcfg"

# Python package name (import name)
PACKAGE_NAME: str = __package__.split(".")[0]  # "curvtools"

# ---------------------------------------------------------------------------   
# Artifact paths generated by this program
# ---------------------------------------------------------------------------
@dataclass(frozen=True)
class MergedTomlArtifactPaths:
    merged_cfgvars: Path
    merged_board: Path
    config_mk_dep: Path
    board_mk_dep: Path

    @classmethod
    def from_curv_paths(cls, curv_paths: CurvPaths) -> MergedTomlArtifactPaths:
        from curvtools.cli.curvcfg.lib.curv_paths.curvpaths import CurvPaths
        if curv_paths is None:
            raise ValueError("curv_paths is None")
        if not isinstance(curv_paths, CurvPaths):
            raise ValueError(f"curv_paths is not a CurvPaths object: {type(curv_paths)}")
        return cls(
            merged_cfgvars=curv_paths["DEFAULT_MERGED_CFGVARS_TOML_PATH"].to_path(),
            merged_board=curv_paths["DEFAULT_MERGED_BOARD_TOML_PATH"].to_path(),
            config_mk_dep=curv_paths["CONFIG_MK_DEP"].to_path(),
            board_mk_dep=curv_paths["BOARD_MK_DEP"].to_path(),
        )

# ---------------------------------------------------------------------------
# Board artifact names
# ---------------------------------------------------------------------------
@dataclass(frozen=True)
class BoardArtifactNames:
    board_svh: str
    board_pkg: str
    board_mk: str
    board_env: str

    @classmethod
    def from_curv_paths(cls, curv_paths: CurvPaths) -> MergedTomlArtifactPaths:
        from curvtools.cli.curvcfg.lib.curv_paths.curvpaths import CurvPaths
        if curv_paths is None:
            raise ValueError("curv_paths is None")
        if not isinstance(curv_paths, CurvPaths):
            raise ValueError(f"curv_paths is not a CurvPaths object: {type(curv_paths)}")
        return cls(
            board_svh=curv_paths["BOARD_SVH"].to_path(),
            board_pkg=curv_paths["BOARD_SVPKG"].to_path(),
            board_mk=curv_paths["BOARD_MK"].to_path(),
            board_env=curv_paths["BOARD_ENV"].to_path(),
        )

# ---------------------------------------------------------------------------
# Config artifact names
# ---------------------------------------------------------------------------
@dataclass(frozen=True)
class ConfigArtifactNames:
    curvcfg_svh: str
    curvcfgpkg_sv: str
    curv_mk: str
    curv_env: str

    @classmethod
    def from_curv_paths(cls, curv_paths: CurvPaths) -> MergedTomlArtifactPaths:
        from curvtools.cli.curvcfg.lib.curv_paths.curvpaths import CurvPaths
        if curv_paths is None:
            raise ValueError("curv_paths is None")
        if not isinstance(curv_paths, CurvPaths):
            raise ValueError(f"curv_paths is not a CurvPaths object: {type(curv_paths)}")
        return cls(
            curvcfgpkg_sv=curv_paths["CURVCFGPKG_SV"].to_path(),
            curvcfg_svh=curv_paths["CURVCFG_SVH"].to_path(),
            curv_mk=curv_paths["CURV_MK"].to_path(),
            curv_env=curv_paths["CURV_ENV"].to_path(),
        )


__all__ = [
    "PROGRAM_NAME",
    "PACKAGE_NAME",
]