import yaml


def load_and_update_eval_config(eval_config_path, cfg, verbose=False):
    """
    Load and update eval config.

    Parameters
    ----------
    eval_config_path : str
        Path to eval config file.
    cfg : dict
        Config dictionary.
    verbose : bool
        If True, print Warnings. Default: False.

    Returns
    -------
    cfg : dict
        Updated eval config dictionary.
    """

    # Load eval config
    with open(eval_config_path, 'r') as f:
        eval_cfg = yaml.load(f, Loader=yaml.Loader)

    # Update config using eval config
    data_cfg = cfg['dataset']
    for key, value in eval_cfg.items():
        if key in ['name', 'sample_size']:
            data_cfg[key] = eval_cfg[key]
            if verbose:
                print(f"Warning: dataset.{key} overridden by eval config to {eval_cfg[key]}.")

        elif key == 'train' and isinstance(value, dict):
            if 'n_views' in value:
                data_cfg[key]['n_views'] = eval_cfg[key]['n_views']
                if verbose:
                    print(f"Warning: dataset.train.n_views overridden by eval config to {eval_cfg[key]['n_views']}.")

            if 'augmentation' in value:
                data_cfg[key]['augmentation'].update(eval_cfg[key]['augmentation'])
                if verbose:
                    print(f"Warning: dataset.train.augmentation overridden by eval config to {eval_cfg[key]['augmentation']}.")

        elif key == 'eval' and isinstance(value, dict):
            data_cfg[key].update(eval_cfg[key])
            if verbose:
                print(f"Warning: dataset.valid overridden by eval config to {eval_cfg[key]}.")

        else:
            raise ValueError("Invalide key found in dataset test config.")

    return cfg