import rich_click as click
from click import Context
from dokker.projects.local import LocalProject
from dokker.loggers.print import PrintLogger
from dokker.deployment import Deployment
from typing import Optional
import os
from .utils import compile_options


@click.command()
@click.option(
    "--name",
    help="The name of the deployment",
    default=None,
    required=False,
    type=click.Choice(compile_options()),
)
@click.pass_context
def down(
    ctx: Context,
    name: Optional[str] = None,
) -> None:
    """
    Initializes a new deployment from a konstruktor template.
    this will create a new folder in the .arkitekt/deployments folder
    with the name of the deployment and the docker-compose.yml file

    """
    if not name:
        options = compile_options()
        if not options:
            raise click.ClickException(
                "No deployments found. Please run arkitekt server init first"
            )

        name = options[0]

    print(f"Running {name}")

    project = LocalProject(
        compose_files=[
            os.path.join(
                os.getcwd(), ".arkitekt", "deployments", name, "docker-compose.yaml"
            )
        ],
    )

    deployment = Deployment(project=project, logger=PrintLogger())
    deployment.up_on_enter = False
    deployment.down_on_exit = True

    with deployment:
        print("Shutting down...")

    print("Done")
