import shutil
from pathlib import Path
from typing import Literal

from .root_location import get_bench_af_root
from .list import _list_objects

_REGISTRY_DIR = Path(__file__).parent.parent


def create_new_model_organism(name: str) -> None:
    """Create a new model organism with the specified name."""
    _create_new_object(name, "models")


def create_new_detector(name: str) -> None:
    """Create a new detector with the specified name."""
    _create_new_object(name, "detectors")


def create_new_environment(name: str) -> None:
    """Create a new environment with the specified name."""
    _create_new_object(name, "environments")


def create_new_config(name: str) -> None:
    """Create a new config with the specified name."""
    _create_new_object(name, "configs")


def _create_new_object(
    name: str, category: Literal["models", "detectors", "environments", "configs"]
) -> None:
    """Create a new object in the specified category."""
    if name in _list_objects(category):
        raise ValueError(f"{category.capitalize()} {name} already exists")
    
    target_root = get_bench_af_root()

    # Copy the entire _defaults/category directory
    shutil.copytree(
        _REGISTRY_DIR / "_defaults" / category, target_root / category / name
    )
