# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/01_cli.ipynb.

# %% auto 0
__all__ = ['cli', 'store', 'get', 'list']

# %% ../nbs/01_cli.ipynb 3
import click
from .core import Sojourner
from pathlib import Path

# %% ../nbs/01_cli.ipynb 4
@click.group()
def cli():
    """Sojourner CLI for managing data blobs."""
    pass


@cli.command()
@click.argument("client_id")
@click.argument("blob_name")
@click.argument("file_path", type=click.Path(exists=True))
@click.option("--manifest", required=True, help="Short description of the blob")
@click.option(
    "--additional", multiple=True, help="Additional metadata in key=value format"
)
def store(client_id, blob_name, file_path, manifest, additional):
    """Store a file as a blob."""
    sj = Sojourner()
    with open(file_path, "rb") as f:
        data = f.read()

    additional_info = dict(item.split("=") for item in additional)

    success = sj.store(client_id, blob_name, data, manifest, **additional_info)
    if success:
        click.echo(f"Successfully stored {blob_name} for client {client_id}")
    else:
        click.echo(f"Failed to store {blob_name}. It may already exist.")


@cli.command()
@click.argument("client_id")
@click.argument("blob_name")
@click.argument("output_path", type=click.Path())
def get(client_id, blob_name, output_path):
    """Retrieve a blob and save it to a file."""
    sj = Sojourner()
    data, metadata = sj.get(client_id, blob_name)

    with open(output_path, "wb") as f:
        f.write(data)

    click.echo(f"Retrieved {blob_name} for client {client_id}")
    click.echo(f"Metadata: {metadata}")


@cli.command()
@click.argument("client_id")
def list(client_id):
    """List all blobs for a given client."""
    sj = Sojourner()
    blobs = sj.list(client_id)

    if blobs:
        click.echo(f"Blobs for client {client_id}:")
        for blob in blobs:
            click.echo(f"- {blob}")
    else:
        click.echo(f"No blobs found for client {client_id}")
