import argparse

from .parser_formatter import CustomHelpFormatter
from .create_toml import ModuleTOMLCreator
from .user_input import UserInput
from .create_files import CreateTemplateFiles

# not a real comment. created for pipeline
if __name__ == "__main__":
    parser = argparse.ArgumentParser(formatter_class=CustomHelpFormatter)
    subparsers = parser.add_subparsers(metavar="command", dest="command")

    # Sub parser declarations
    subparser_create = subparsers.add_parser(
        "create", help="create a new python module template"
    )

    # subparser_create arguments
    subparser_create.add_argument("path", help="path to target directory")

    subparser_create.add_argument(
        "--author", action="store_true", default=False, help="toggle author input"
    )

    subparser_create.add_argument(
        "--build-system",
        action="store_true",
        default=False,
        help="toggle build-system input",
    )
    subparser_create.add_argument(
        "--dependencies",
        action="store_true",
        default=False,
        help="toggle dependency input",
    )
    subparser_create.add_argument("--repo", help="set repository")

    subparser_create.add_argument(
        "-r", "--requirements", help="items to add to requirements.txt"
    )
    subparser_create.add_argument(
        "-i", "--ignore", nargs="*", help="items to add to .gitignore"
    )

    # add a --dependencies flag that is bool and toggles the dependency input for streamlining ui
    # set to true to include initial dependencies. default = False

    # add --build-system flag that is bool and toggles build system input
    # set to true to include and default = False. Defaults to 'hatchling' build system

    args = parser.parse_args()

    match args.command:
        case "create":
            # Get user input
            collect_input = UserInput(
                build_system=args.build_system,
                dependencies=args.dependencies,
                author=args.author,
                repo=args.repo,
            )

            # User input dictionary
            inputs = collect_input.run()

            # Check and set build backend system
            if inputs["Build System"] == "hatchling":
                build_system_backend = "hatchling.build"

            if args.repo:
                homepage = f"""https://{args.repo}.com/{inputs["Module Name"].replace("_", "-")}"""
                bug_tracker = f"""https://{args.repo}.com/{inputs["Module Name"].replace("_", "-")}/issues"""
            else:
                homepage = inputs["Homepage"]
                bug_tracker = inputs["Bug Tracker"]

            # Create the basic files
            ctf = CreateTemplateFiles(path=args.path)

            ctf.make_src_directory(inputs["Module Name"])
            ctf.make_readme(inputs["Readme"])
            ctf.make_requirements(args.requirements)
            ctf.make_license(inputs["License"], inputs["Authors"][0]["name"])
            ctf.make_gitignore(args.ignore)
            ctf.make_gitlab_ci()
            ctf.make_tests_directory()
            ctf.make_conftest()

            # Create pyproject.toml
            mtc = ModuleTOMLCreator(
                buildSystem=inputs["Build System"],
                buildSystemBackend=build_system_backend,
                moduleName=inputs["Module Name"],
                moduleVersion=inputs["Module Version"],
                moduleAuthors=inputs["Authors"],
                description=inputs["Description"],
                readme=inputs["Readme"],
                requiredPythonVersion=inputs["Required Python Version"],
                dependencies=inputs["Dependencies"],
                license=inputs["License"],
                operatingSystem=inputs["Operating System"],
                homepage=homepage,
                bugTracker=bug_tracker,
                savePath=args.path,
            )

            mtc.save()
