#!/usr/bin/env python
import shutil
import subprocess
import sys
from pathlib import Path

try:
    from commitizen.cz.utils import get_backup_file_path
except ImportError as error:
    print("could not import commitizen:")
    print(error)
    exit(1)


def prepare_commit_msg(commit_msg_file: str) -> int:
    # check if the commit message needs to be generated using commitizen
    exit_code = subprocess.run(
        [
            "cz",
            "check",
            "--commit-msg-file",
            commit_msg_file,
        ],
        capture_output=True,
    ).returncode
    if exit_code == 0:
        return 0

    backup_file = Path(get_backup_file_path())
    if backup_file.is_file():
        # confirm if commit message from backup file should be reused
        answer = input("retry with previous message? [y/N]: ")
        if answer.lower() == "y":
            shutil.copyfile(backup_file, commit_msg_file)
            return 0

    # use commitizen to generate the commit message
    exit_code = subprocess.run(
        [
            "cz",
            "commit",
            "--dry-run",
            "--write-message-to-file",
            commit_msg_file,
        ],
        stdin=sys.stdin,
        stdout=sys.stdout,
    ).returncode
    if exit_code:
        return exit_code

    # write message to backup file
    shutil.copyfile(commit_msg_file, backup_file)
    return 0


if __name__ == "__main__":
    # make hook interactive by attaching /dev/tty to stdin
    with open("/dev/tty") as tty:
        sys.stdin = tty
        exit_code = prepare_commit_msg(sys.argv[1])
        exit(exit_code)
