# AUTOGENERATED! DO NOT EDIT! File to edit: ../../../pts/mod/cmds/01_new_repo.pct.py.

# %% top_export
from pathlib import Path
import subprocess
from datetime import datetime

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,
    copy_from_path: bool = False,
    creator_hostname: str|None = None,
    creation_timestamp_utc: datetime|None = None,
    initialise_git: bool = True,
    verbose: bool = False,
):
    """
    Create a new repoyard repository.

    Args:
        config_path: The path to the repoyard config file.
        storage_location: The storage location to create the new repository in.
        repo_name: The name of the new repository.
        from_path: The path to a local directory to move into repoyard as a new repository.
        copy_from_path: Whether to copy the contents of the from_path into the new repository.
        creator_hostname: The hostname of the creator of the new repository.
        creation_timestamp_utc: The timestamp of the new repository. If not provided, the current UTC timestamp will be used.
        initialise_git: Whether to initialise a git repository in the new repository.
        verbose: Whether to print verbose output.

    Returns:
        The full name of the new repository.
    """
    
    # AUTOGENERATED! DO NOT EDIT! File to edit: ../../../pts/mod/cmds/01_new_repo.pct.py.
    
    # %% auto 0
    __all__ = ['config', 'repo_meta', 'repo_path', 'repo_data_path', 'repo_conf_path']
    
    # %% ../../../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
        
    if from_path is None and copy_from_path:
        raise ValueError("`from_path` must be provided if `copy_from_path` is True.")
    
    from .._utils import get_hostname
    if creator_hostname is None:
        creator_hostname = get_hostname()
    
    # %% ../../../pts/mod/cmds/01_new_repo.pct.py 13
    from .._models import RepoMeta
    repo_meta = RepoMeta.create(
        config,
        name=repo_name,
        storage_location_name=storage_location,
        groups=[],
        creator_hostname=creator_hostname,
        creation_timestamp_utc=creation_timestamp_utc,
    )
    
    repo_meta.save(config)
    
    # %% ../../../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:
        if copy_from_path:
            import shutil
            shutil.copytree(from_path, repo_data_path) #TESTREF: test_new_repo_copy_from_path
        else:
            from_path.rename(repo_data_path)
    else:
        repo_data_path.mkdir(parents=True, exist_ok=True)
    
    # %% ../../../pts/mod/cmds/01_new_repo.pct.py 17
    (repo_conf_path / ".repoyard_exclude").write_text(const.DEFAULT_REPOYARD_EXCLUDE);
    
    # %% ../../../pts/mod/cmds/01_new_repo.pct.py 19
    if initialise_git and not (repo_data_path / '.git').exists():
        if verbose: print("Initialising git repository")
        res = subprocess.run(
            ["git", "init"], 
            check=True, 
            cwd=repo_data_path,
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL
        )
        if res.returncode != 0:
            if verbose: print("Warning: Failed to initialise git repository")
    
    # %% ../../../pts/mod/cmds/01_new_repo.pct.py 21
    from .._models import refresh_repoyard_meta
    refresh_repoyard_meta(config)
    return repo_meta.full_name;