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

# %% top_export
from pathlib import Path
import subprocess
import os
from typing import Literal, Any

from repoyard._utils import get_repo_full_name_from_sub_path
from repoyard.config import get_config, StorageType
from repoyard import const

# %% top_export
class RepoNameConflict(Exception): pass

def modify_repometa(
    config_path: Path,
    repo_full_name: str,
    modifications: dict[str, Any] = {},
):
    """
    """
    
    # AUTOGENERATED! DO NOT EDIT! File to edit: ../../../pts/mod/cmds/05_modify_repometa.pct.py.
    
    # %% auto 0
    __all__ = ['config', 'repoyard_meta', 'repo_meta', 'modified_repo_meta', 'repo_group_configs']
    
    # %% ../../../pts/mod/cmds/05_modify_repometa.pct.py 12
    config = get_config(config_path)
    
    # %% ../../../pts/mod/cmds/05_modify_repometa.pct.py 15
    from .._models import get_repoyard_meta, RepoMeta
    repoyard_meta = get_repoyard_meta(config)
    
    if repo_full_name not in repoyard_meta.by_full_name:
        raise ValueError(f"Repo '{repo_full_name}' not found.")
    
    repo_meta = repoyard_meta.by_full_name[repo_full_name]
    
    # %% ../../../pts/mod/cmds/05_modify_repometa.pct.py 17
    modified_repo_meta = RepoMeta(**{
        **repo_meta.model_dump(),
        **modifications
    })
    
    # %% ../../../pts/mod/cmds/05_modify_repometa.pct.py 19
    #TESTREF: test_modify_repometa_unique_names
    from .._models import get_repo_group_configs
    
    # Construct modified repo_metas list
    _old_repo_meta = repoyard_meta.by_full_name[repo_full_name]
    _repo_metas = list(repoyard_meta.repo_metas)
    _repo_metas.remove(_old_repo_meta)
    _repo_metas.append(modified_repo_meta)
    
    repo_group_configs = get_repo_group_configs(config, _repo_metas)
    for g in modified_repo_meta.groups:
        repo_group_config = repo_group_configs[g]
        repo_metas_in_group = [rm for rm in _repo_metas if g in modified_repo_meta.groups]
    
        if repo_group_config.unique_repo_names:
            name_counts = {repo_meta.name: 0 for repo_meta in repo_metas_in_group}
            for repo_meta in repo_metas_in_group:
                name_counts[repo_meta.name] += 1
            duplicate_names = [(name, count) for name, count in name_counts.items() if count > 1]
            if duplicate_names:
                names_str = ", ".join(f"'{name}' (count: {count})" for name, count in duplicate_names)
                raise RepoNameConflict(f"Error modifying repo meta for '{repo_full_name}':\n"
                                 f"Repo is in group '{g}' which requires unique names. After the modification, the following name(s) appear multiple times in this group: {names_str}.")
    
    # %% ../../../pts/mod/cmds/05_modify_repometa.pct.py 21
    modified_repo_meta.save(config)
    
    # %% ../../../pts/mod/cmds/05_modify_repometa.pct.py 24
    from .._models import refresh_repoyard_meta
    refresh_repoyard_meta(config)