import typer
from typing_extensions import Annotated
from dektools.shell import shell_wrapper
from dektools.file import remove_path, write_file, read_text
from dektools.typer import command_mixin
from dektools.url import split_auth_url

app = typer.Typer(add_completion=False)


@app.command()
def config(
        update: Annotated[bool, typer.Option("--update/--no-update")] = False,
        cache: Annotated[bool, typer.Option("--cache/--no-cache")] = True,
        redirect: Annotated[bool, typer.Option("--redirect/--no-redirect")] = False,
):
    shell_wrapper(f"pdm config check_update {'true' if update else 'false'}")
    shell_wrapper(f"pdm config install.cache {'on' if cache else 'off'}")

    # disable mirror redirect
    from unearth import collector
    path_target = collector.__file__
    contents = (
        "        headers={",
        "        allow_redirects=False, headers={"
    )
    write_file(path_target, read_text(path_target).replace(*contents[::-1 if redirect else 1]))


@command_mixin(app)
def install(args):
    shell_wrapper(f'pdm install {args}')


@app.command()
def clear():
    from .core import get_pdm_cache_dir
    shell_wrapper(f'pdm cache clear')
    remove_path(get_pdm_cache_dir())


@app.command()
def clear_hash():
    from .core import get_pdm_cache_dir_hash
    remove_path(get_pdm_cache_dir_hash())


default_pypi_name = 'private'


@app.command()
def login(url, name: Annotated[str, typer.Argument()] = default_pypi_name):
    url, username, password = split_auth_url(url)
    shell_wrapper(f'pdm config pypi.{name}.url {url}')
    if url.startswith('https:'):
        shell_wrapper(f'pdm config pypi.{name}.verify_ssl true')
    shell_wrapper(f'pdm config pypi.{name}.username "{username}"')
    shell_wrapper(f'pdm config pypi.{name}.password "{password}"')


@app.command()
def logout(name: Annotated[str, typer.Argument()] = default_pypi_name):
    shell_wrapper(f'pdm config pypi.{name} --delete')
