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

# %% top_export
from pathlib import Path
from enum import Enum

from repoyard._utils.sync_helper import sync_helper, SyncSetting, SyncDirection
from repoyard._models import SyncStatus, RepoPart
from repoyard.config import get_config, StorageType
from repoyard._utils import async_throttler
from repoyard._utils import check_interrupted, enable_soft_interruption, SoftInterruption
from repoyard import const

async def sync_repo(
    config_path: Path,
    repo_full_name: str,
    sync_direction: SyncDirection|None = None,
    sync_setting: SyncSetting = SyncSetting.CAREFUL,
    sync_choices: list[RepoPart]|None = None,
    verbose: bool = False,
    show_rclone_progress: bool = False,
    soft_interruption_enabled: bool = True,
) -> dict[RepoPart, SyncStatus]:
    """
    Syncs a repo with its remote.
    
    Args:
        config_path: Path to the repoyard config file.
        repo_full_name: Full name of the repository to sync.
        sync_direction: Direction of sync.
        sync_setting: SyncSetting option (SAFE, CAREFUL, FORCE).
        sync_choices: List of RepoPart specifying what to sync. If None, all parts are synced.
        force: Force syncing, possibly overwriting changes.
        verbose: Print verbose output during sync.
        show_rclone_progress: Show rclone progress during sync.
    """
    
    # AUTOGENERATED! DO NOT EDIT! File to edit: ../../../pts/mod/cmds/03_sync_repo.pct.py.
    
    # %% auto 0
    __all__ = ['config', 'repoyard_meta', 'repo_meta', 'sl_config', 'local_sync_backups_path', 'remote_sync_backups_path',
               'sync_results', 'sync_part']
    
    # %% ../../../pts/mod/cmds/03_sync_repo.pct.py 12
    config = get_config(config_path)
    if sync_choices is None:
        sync_choices = [repo_part for repo_part in RepoPart]
    
    if soft_interruption_enabled:
        enable_soft_interruption()
    
    # %% ../../../pts/mod/cmds/03_sync_repo.pct.py 15
    from .._models import get_repoyard_meta
    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/03_sync_repo.pct.py 17
    if repo_meta.get_storage_location_config(config).storage_type == StorageType.LOCAL:
        pass
        return #|return_line
    
    # %% ../../../pts/mod/cmds/03_sync_repo.pct.py 19
    if verbose:
        print(f"Syncing repo {repo_full_name} at {repo_meta.storage_location}.")
    
    # %% ../../../pts/mod/cmds/03_sync_repo.pct.py 21
    sl_config = repo_meta.get_storage_location_config(config)
    local_sync_backups_path = config.local_sync_backups_path
    remote_sync_backups_path = sl_config.store_path / const.REMOTE_BACKUP_REL_PATH
    
    # %% ../../../pts/mod/cmds/03_sync_repo.pct.py 23
    sync_results = {}
    
    if check_interrupted(): raise SoftInterruption()
    
    sync_part = RepoPart.META
    if sync_part in sync_choices:
        if verbose: print(f"Syncing {sync_part.value}.")
        sync_results[RepoPart.META] = await sync_helper(
            rclone_config_path=config.rclone_config_path,
            sync_direction=sync_direction,
            sync_setting=sync_setting,
            local_path=repo_meta.get_local_repometa_path(config),
            local_sync_record_path=repo_meta.get_local_sync_record_path(config, sync_part),
            remote=repo_meta.storage_location,
            remote_path=repo_meta.get_remote_repometa_path(config),
            remote_sync_record_path=repo_meta.get_remote_sync_record_path(config, sync_part),
            local_sync_backups_path=local_sync_backups_path,
            remote_sync_backups_path=remote_sync_backups_path,
            verbose=verbose,
            show_rclone_progress=show_rclone_progress,
        )
    
    # %% ../../../pts/mod/cmds/03_sync_repo.pct.py 26
    if check_interrupted(): raise SoftInterruption()
    
    sync_part = RepoPart.CONF
    if sync_part in sync_choices:
        if verbose: print("Syncing", sync_part.value)
        sync_results[sync_part] = await sync_helper(
            rclone_config_path=config.rclone_config_path,
            sync_direction=sync_direction,
            sync_setting=sync_setting,
            local_path=repo_meta.get_local_repoconf_path(config),
            local_sync_record_path=repo_meta.get_local_sync_record_path(config, sync_part),
            remote=repo_meta.storage_location,
            remote_path=repo_meta.get_remote_repoconf_path(config),
            remote_sync_record_path=repo_meta.get_remote_sync_record_path(config, sync_part),
            local_sync_backups_path=local_sync_backups_path,
            remote_sync_backups_path=remote_sync_backups_path,
            verbose=verbose,
            show_rclone_progress=show_rclone_progress,
        )
    
    # %% ../../../pts/mod/cmds/03_sync_repo.pct.py 29
    _repoyard_include_path = repo_meta.get_local_repoconf_path(config) / ".repoyard_include"
    _repoyard_exclude_path = repo_meta.get_local_repoconf_path(config) / ".repoyard_exclude"
    _repoyard_filters_path = repo_meta.get_local_repoconf_path(config) / ".repoyard_filters"
    
    _repoyard_include_path = _repoyard_include_path if _repoyard_include_path.exists() else None
    _repoyard_exclude_path = _repoyard_exclude_path if _repoyard_exclude_path.exists() else None
    _repoyard_filters_path = _repoyard_filters_path if _repoyard_filters_path.exists() else None
    
    # %% ../../../pts/mod/cmds/03_sync_repo.pct.py 31
    if check_interrupted(): raise SoftInterruption()
    
    sync_part = RepoPart.DATA
    if sync_part in sync_choices:
        if verbose: print("Syncing", sync_part.value)
        sync_results[sync_part] = await sync_helper(
            rclone_config_path=config.rclone_config_path,
            sync_direction=sync_direction,
            sync_setting=sync_setting,
            local_path=repo_meta.get_local_repodata_path(config),
            local_sync_record_path=repo_meta.get_local_sync_record_path(config, sync_part),
            remote=repo_meta.storage_location,
            remote_path=repo_meta.get_remote_repodata_path(config),
            remote_sync_record_path=repo_meta.get_remote_sync_record_path(config, sync_part),
            local_sync_backups_path=local_sync_backups_path,
            remote_sync_backups_path=remote_sync_backups_path,
            include_path=_repoyard_include_path,
            exclude_path=_repoyard_exclude_path,
            filters_path=_repoyard_filters_path,
            verbose=verbose,
            show_rclone_progress=show_rclone_progress,
        )
    
    # %% ../../../pts/mod/cmds/03_sync_repo.pct.py 34
    if RepoPart.META in sync_choices:
        from repoyard._models import refresh_repoyard_meta
        refresh_repoyard_meta(config)
    return sync_results;