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

# %% top_export
from pathlib import Path

from repoyard._utils.bisync_helper import bisync_helper, SyncSetting
from repoyard.config import get_config, StorageType
from repoyard import const

def sync_repometas(
    config_path: Path,
    repo_full_names: list[str]|None = None,
    storage_locations: list[str]|None = None,
    sync_setting: SyncSetting = SyncSetting.BISYNC,
    force: bool = False,
):
    """
    """
    
    # AUTOGENERATED! DO NOT EDIT! File to edit: ../../../../../../../../../Users/lukastk/dev/2025-11-09_00__repoyard/pts/mod/cmds/03_sync_repometas.pct.py.
    
    # %% auto 0
    __all__ = ['config']
    
    # %% ../../../../../../../../../Users/lukastk/dev/2025-11-09_00__repoyard/pts/mod/cmds/03_sync_repometas.pct.py 11
    config = get_config(config_path)
    
    if repo_full_names is not None and storage_locations is not None:
        raise ValueError("Cannot provide both `repo_full_names` and `storage_locations`.")
    
    # %% ../../../../../../../../../Users/lukastk/dev/2025-11-09_00__repoyard/pts/mod/cmds/03_sync_repometas.pct.py 14
    # Sync by storage location
    if repo_full_names is None:
        # Get all paths to all storage locations to sync
        storage_locations_to_sync = [sl_name for sl_name in config.storage_locations if config.storage_locations[sl_name].storage_type != StorageType.LOCAL]
        if storage_locations is not None:
            storage_locations_to_sync = [sl_name for sl_name in storage_locations_to_sync if sl_name in storage_locations]
            
        # Sync each storage location
        for sl_name in storage_locations_to_sync:
            local_path = config.local_store_path / sl_name
            remote_path = config.storage_locations[sl_name].store_path
            bisync_helper(
                rclone_config_path=config.rclone_config_path,
                sync_setting=sync_setting,
                local_path=local_path,
                remote=sl_name,
                remote_path=remote_path,
                force=force,
                include=[f"/*/{const.REPO_METAFILE_REL_PATH}"]
            )
                
    # Sync by repo name     
    else:
        from repoyard._repos import get_repoyard_meta
        repoyard_meta = get_repoyard_meta(config)
        
        # Ensure all repo names are present in repoyard_meta
        for repo_full_name in repo_full_names:
            if repo_full_name not in repoyard_meta.by_full_name:
                raise ValueError(f"Repo '{repo_full_name}' not found.")
            
        # Sync each storage location
        for sl_name in config.storage_locations:
            if config.storage_locations[sl_name].storage_type == StorageType.LOCAL: continue
            sl_repo_full_names = [repo_full_name for repo_full_name in repo_full_names if repoyard_meta.by_full_name[repo_full_name].storage_location == sl_name]
            local_path = config.local_store_path / sl_name
            remote_path = config.storage_locations[sl_name].store_path
            if len(sl_repo_full_names) == 0: continue
            includes = [
                f"/{repo_full_name}/{const.REPO_METAFILE_REL_PATH}"
                for repo_full_name in sl_repo_full_names
            ]
            bisync_helper(
                rclone_config_path=config.rclone_config_path,
                sync_setting=sync_setting,
                local_path=local_path,
                remote=sl_name,
                remote_path=remote_path,
                force=force,
                include=includes,
            )
    
    # %% ../../../../../../../../../Users/lukastk/dev/2025-11-09_00__repoyard/pts/mod/cmds/03_sync_repometas.pct.py 16
    from .._repos import refresh_repoyard_meta
    refresh_repoyard_meta(config)