# AUTOGENERATED! DO NOT EDIT! File to edit: ../../../../../../../../../Users/lukastk/dev/2025-11-09_00__repoyard/pts/mod/cmds/01_new_repo.pct.py.

# %% top_export
from pathlib import Path
import subprocess

from repoyard import const
from repoyard.config import StorageType

def new_repo(
    config_path: Path,
    storage_location: str|None = None,
    repo_name: str|None = None,
    from_path: Path|None = None,
    creator_hostname: str|None = None,
    initialise_git: bool = True,
):
    """
    Create a new repoyard repository.
    """
    
    # AUTOGENERATED! DO NOT EDIT! File to edit: ../../../../../../../../../Users/lukastk/dev/2025-11-09_00__repoyard/pts/mod/cmds/01_new_repo.pct.py.
    
    # %% auto 0
    __all__ = ['config', 'repo_meta', 'repo_path', 'repo_data_path', 'repo_conf_path']
    
    # %% ../../../../../../../../../Users/lukastk/dev/2025-11-09_00__repoyard/pts/mod/cmds/01_new_repo.pct.py 11
    from ..config import get_config
    config = get_config(config_path)
        
    if storage_location is None:
        storage_location = config.default_storage_location
        
    if storage_location not in config.storage_locations:
        raise ValueError(f"Invalid storage location: {storage_location}. Must be one of: {', '.join(config.storage_locations)}.")
        
    if repo_name is None and from_path is None:
        raise ValueError("Either `repo_name` or `from_path` must be provided.")
    
    if from_path is not None:
        from_path = Path(from_path).expanduser().resolve()
        
    if from_path is not None and repo_name is None:
        repo_name = from_path.name
        
    from .._utils import get_hostname
    if creator_hostname is None:
        creator_hostname = get_hostname()
    
    # %% ../../../../../../../../../Users/lukastk/dev/2025-11-09_00__repoyard/pts/mod/cmds/01_new_repo.pct.py 13
    from .._repos import RepoMeta
    repo_meta = RepoMeta(
        name=repo_name,
        storage_location=storage_location,
        groups=[],
        creator_hostname=creator_hostname,
    )
    repo_meta.save(config)
    
    # %% ../../../../../../../../../Users/lukastk/dev/2025-11-09_00__repoyard/pts/mod/cmds/01_new_repo.pct.py 15
    repo_path = repo_meta.get_local_path(config)
    repo_data_path = repo_meta.get_local_repodata_path(config)
    repo_conf_path = repo_meta.get_local_repoconf_path(config)
    repo_path.mkdir(parents=True, exist_ok=True)
    repo_conf_path.mkdir(parents=True, exist_ok=True)
    
    if from_path is not None:
        from_path.rename(repo_data_path)
    else:
        repo_data_path.mkdir(parents=True, exist_ok=True)
    
    # %% ../../../../../../../../../Users/lukastk/dev/2025-11-09_00__repoyard/pts/mod/cmds/01_new_repo.pct.py 17
    (repo_conf_path / ".repoyard_exclude").write_text(const.DEFAULT_REPOYARD_EXCLUDE);
    
    # %% ../../../../../../../../../Users/lukastk/dev/2025-11-09_00__repoyard/pts/mod/cmds/01_new_repo.pct.py 19
    if initialise_git:
        subprocess.run(["git", "init"], check=True, cwd=repo_data_path)
    
    # %% ../../../../../../../../../Users/lukastk/dev/2025-11-09_00__repoyard/pts/mod/cmds/01_new_repo.pct.py 21
    from .._repos import refresh_repoyard_meta
    refresh_repoyard_meta(config)
    return repo_meta.full_name;