#!/usr/bin/env python3

import argparse
import os
import subprocess
import sys
from pathlib import PurePath
from typing import Dict, List, cast

import lister


# This could use tools/python_tools.py in future?
EXCLUDE_FILES = [
    "tools/fetch-pull-request",
    "tools/fetch-rebase-pull-request",
    "tools/check-branch",
]

project_mypy_args: Dict[str, List[str]] = {
    "zulipterminal": [],
    "tests": ["--implicit-reexport"],
    "tools": [],
}
python_project_folders = list(project_mypy_args)

TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
os.chdir(os.path.dirname(TOOLS_DIR))

sys.path.append(os.path.dirname(TOOLS_DIR))

parser = argparse.ArgumentParser(description="Run mypy on files tracked by git.")

parser.add_argument(
    "targets",
    nargs="*",
    default=[],
    help="""files and directories to include in the result.
            If this is not specified, the current directory is used""",
)

parser.add_argument(
    "-m",
    "--modified",
    action="store_true",
    default=False,
    help="list only modified files",
)

parser.add_argument(
    "-a",
    "--all",
    dest="all",
    action="store_true",
    default=False,
    help="""run mypy on all python files,
            ignoring the exclude list. This is useful if you have to
            find out which files fail mypy check.""",
)

parser.add_argument(
    "--scripts-only",
    dest="scripts_only",
    action="store_true",
    default=False,
    help="""Only type check extensionless python scripts""",
)

args = parser.parse_args()

files_dict = cast(
    Dict[str, List[str]],
    lister.list_files(
        targets=args.targets,
        ftypes=["py"],
        use_shebang=True,
        modified_only=args.modified,
        group_by_ftype=True,
        exclude=EXCLUDE_FILES,
    ),
)


pyi_files = set(files_dict["pyi"])
python_files = [
    fpath
    for fpath in files_dict["py"]
    if not fpath.endswith(".py") or fpath + "i" not in pyi_files
]

repo_python_files: Dict[str, List[str]] = {
    folder: [] for folder in python_project_folders
}

for file_path in python_files:
    repo = PurePath(file_path).parts[0]
    filename = PurePath(file_path).parts[-1]
    if repo in repo_python_files:
        repo_python_files[repo].append(file_path)

mypy_command = "mypy"

extra_args: List[str] = []

# run mypy
status = 0
for repo, python_files in repo_python_files.items():
    print(f"Running mypy for `{repo}`.", flush=True)
    repo_args = project_mypy_args[repo]
    if python_files:
        result = subprocess.call([mypy_command] + extra_args + repo_args + python_files)
        if result != 0:
            status = result
    else:
        print("There are no files to run mypy on.")
sys.exit(status)
