# -*- coding: utf-8 -*-
#
# Copyright 2018 - Swiss Data Science Center (SDSC)
# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
# Eidgenössische Technische Hochschule Zürich (ETHZ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
r"""Show information about objects in current repository.

Siblings
~~~~~~~~

In situations when multiple outputs have been generated by a single
``renku run`` command, the siblings can be discovered by running
``renku show siblings PATH`` command.

Assume that the following graph represents relations in the repository.

.. code-block:: text

          D---E---G
         /     \
    A---B---C   F

Then the following outputs would be shown.

.. code-block:: console

   $ renku show siblings C
   C
   D
   $ renku show siblings G
   F
   G
   $ renku show siblings A
   A

"""

import click

from ._client import pass_local_client
from ._git import with_git
from ._graph import Graph


@click.group()
def show():
    """Show information about objects in current repository.

    NOTE: The command produces machine readable output.
    """


@show.command()
@click.option('--revision', default='HEAD')
@click.argument(
    'paths', type=click.Path(exists=True, dir_okay=False), nargs=-1
)
@pass_local_client
@click.pass_context
@with_git(clean=False, commit=False)
def siblings(ctx, client, revision, paths):
    """Show siblings for given paths."""
    graph = Graph(client)
    graph.build_status(revision=revision)

    nodes = {
        graph.add_file(graph.normalize_path(path), revision=revision)
        for path in paths
    }
    siblings_ = set(nodes)
    for node in nodes:
        siblings_ |= graph.siblings(node)

    for _, path in siblings_:
        click.echo(graph._format_path(path))
