import click
import subprocess as sp
import os

from app.helper import install_available


@click.group()
def cli():
    pass


@cli.command("setup")
@click.option("--path", required=True)
@click.option("--api_server", required=True)
def setup(path, api_server):
    os.chdir(path)

    repo_name = "ucloud-cli"

    # Clone main repo
    sp.check_output(['git', 'clone', '--single-branch', '--branch', 'wip',
                    f'https://code.ungleich.ch/ucloud/{repo_name}.git'])

    # Create .env file
    with open(os.path.join(repo_name, ".env"), "w") as f:
        content = (
            f"UCLOUD_API_SERVER={api_server}\n"
        )
        f.writelines(content)

    # Clone Common and Etcd Wrapper
    # clone_common(path=repo_name)
    # clone_etcd_wrapper(path=repo_name)

    # Copy Etcd Wrapper inside ucloud_common as ucloud_common
    # also needs etcd wrapper
    # shutil.copytree(
    #     src=os.path.join(repo_name, "etcd3_wrapper"),
    #     dst=os.path.join(repo_name, "ucloud_common", "etcd3_wrapper"),
    # )
    
    install_available(repo_name)
    
    # Create virtualenv with site-packages enabled and install all dependencies
    # sp.check_output(['pipenv','--site-packages', '--three'], cwd=repo_name)
    sp.check_output(['pipenv', 'install'], cwd=repo_name)