import shutil
from pathlib import Path
from typing import Tuple

from dev_tools.build.freeze.cx import NanoBuildOptions, setup_nano


def freeze_build_app(development_build: bool = False, skip_signing: bool = False) -> Tuple[Path, str]:
    NanoBuildOptions.DEVELOPMENT_BUILD = development_build
    NanoBuildOptions.SKIP_SIGNING = skip_signing

    setup_nano()
    tag_version, git_version = NanoBuildOptions.TAG_VERSION, NanoBuildOptions.GIT_VERSION
    assert tag_version
    exe_dir = NanoBuildOptions.EXE_DIR
    assert exe_dir.is_dir()

    # rename it to reflect the build (compress it later)
    exe_dir_name = exe_dir.name.rpartition("-")[0].partition(".")[-1]
    _dev_tag = "-dev" if development_build else ""
    versioned_exe_dir = exe_dir.parent / f"clarabot-nano{_dev_tag}-{tag_version}{git_version}-{exe_dir_name}"
    if versioned_exe_dir.is_dir():
        shutil.rmtree(versioned_exe_dir)
    exe_dir.rename(versioned_exe_dir)

    return versioned_exe_dir, tag_version + git_version
