#!/usr/bin/env python3
"""Updates the dates in the changelog to today's date, then
launch the `git citool` GUI to create a commit of it, and uses
the `gh` tool to file a pull request.
To be run after the GA version of emissary has been tagged.
change the appropriate files, then launch the `git citool` GUI to
create a commit of it.
"""

from os import getenv
import datetime
import fileinput
import os
import re
import sys
from contextlib import contextmanager
from typing import Generator
from shutil import which

from lib import base_version, build_version, git_add, git_check_clean, re_ga, vX, vY, get_gh_repo
from lib.uiutil import Checker, CheckResult, run
from lib.uiutil import run_txtcapture as run_capture
from lib import ansiterm
from lib.gitutil import has_open_pr


def main(next_ver: str, today: datetime.date, quiet: bool=False, commit: bool = True) -> int:
    """This edits several files (the Git directory "tree"), then launches
    the `git citool` GUI to commit them.  This _should_ be an utterly
    trivial and readable list of
        for line in fileinput.FileInput("FILENAME", inplace=True):
            # edit 'line' as appropriate
            sys.stdout.write(line)
        git_add("FILENAME")
    blocks.  However, the block to edit the CHANGELOG.md file is unfortunately a
    touch more complex, because it has to deal with parsing the file in to
    sections and buffering the sections... maybe line-oriented processing wasn't
    the best choice for that file.
    """

    if not quiet:
        print()
        print(f'Doing basic updates for v{next_ver}...')
        print()

    if which("gh") is None:
        print("gh tool is not installed.")
        print("Please install the tool and rerun this script:")
        print("https://github.com/cli/cli#installation")
        return 1

    warning = """
 ==> Warning: FIXME: This script does not have the property that if
     something goes wrong, you can just restart it; put another way:
     it does not have the property that each step is idempotent.
     If something does go wrong, then you'll have to address it, then
     resume where the script left off by going through the checklist
     manually (or by commenting out the already-completed steps).
"""
    print(f"{ansiterm.sgr.fg_red}{warning}{ansiterm.sgr}")

    # This context manager and check function are pretty much just to produce
    # a nice list of steps...
    checker = Checker()

    @contextmanager
    def check(name: str) -> Generator[CheckResult, None, None]:
        with checker.check(name) as subcheck:
            # time.sleep(1)  # it's stupid, but honestly the delay makes the output more readable
            yield subcheck

    if not getenv("GITHUB_TOKEN"):
        run(["gh", "auth", "login"])

    m = re_ga.match(next_ver)
    assert m
    release_branch = f"release/v{m[vX]}.{m[vY]}"
    with check(f"Checking that {release_branch} exists"):
        run(["git", "rev-parse", "--verify", release_branch])
    if not checker.ok:
        checker.ok = True
        with check(f"Creating {release_branch}"):
            run(["git", "checkout", "-b", release_branch])
            run(["git", "push", "-u", "origin", release_branch])
        if not checker.ok:
            print(f"Could not create {release_branch}")
            print("Please create this branch then rerun this script")
            return 1

    user = os.getenv("USER")
    if user is None or user == '':
        user = 'unknownuser'
    workbranch = f"{user}/v{next_ver}/changelog-updates"
    with check(f"Creating new branch {workbranch}"):
        run(["git", "fetch", "--tags"])
        run(["git", "checkout", f"v{next_ver}", "-b", workbranch])
    if not checker.ok:
        return 1

    with check(f"Updating CHANGELOG.md with date for {next_ver}..."):
        found = False
        for line in fileinput.FileInput("CHANGELOG.md", inplace=True):
            if not found and line.startswith(f"## [{next_ver}]"):
                line = f"## [{next_ver}] {today.strftime('%B %d, %Y')}\n"
                found = True

            sys.stdout.write(line)

        if not found:
            raise Exception(f"Changelog entry for {next_ver} not found. Was releng/start-update-version run?")

        git_add("CHANGELOG.md")

    if checker.ok and commit:
        with check(f"Committing changes..."):
            gitdir = run_capture(['git', 'rev-parse', '--git-dir'])
            with open(os.path.join(gitdir, 'GITGUI_MSG'), 'w') as msgfile:
                msgfile.write(f"Update for v{next_ver}\n")
            if getenv("AMBASSADOR_RELENG_NO_GUI"):
                run(['git', 'commit', '-am', f'Update for v{next_ver}'])
            else:
                run(['git', 'citool'])
            run(["git", "push", "-u", "origin", workbranch])
        if not checker.ok:
            return 1

        with check(f"Creating PR for branch {workbranch}"):
            # TODO: dont hardcode owners
            pr_body = """Changelog date updates for {next_ver}

Reviewers: The only changes in this PR should be updating the changelog entry for {next_ver} to the date it was released.

If there are any other changes, the PR creator should note the reason."""
            curr_release_branch = f"rel/v{next_ver}"
            target_branch = release_branch
            if has_open_pr(get_gh_repo(), release_branch, curr_release_branch):
                target_branch = curr_release_branch
            run(["gh", "pr", "create",
                    "--repo", get_gh_repo(),
                    "--base", target_branch,
                    "--title", f"[v{next_ver}] Changelog date update",
                    "--body", pr_body.format(next_ver=next_ver),
                    "--reviewer", "kflynn,rhs,esmet,acookin"])

    if checker.ok:
        if getenv("AMBASSADOR_RELENG_NO_GUI"):
            run(["gh", "pr", "view", workbranch, "--repo", get_gh_repo()])
        else:
            run(["gh", "pr", "view", workbranch, "--repo", get_gh_repo(), "--web"])
        return 0
    else:
        return 1


if __name__ == '__main__':
    args = sys.argv[1:]

    quiet = False
    commit = True

    while args and args[0].startswith("--"):
        if args[0] == '--quiet':
            quiet = True
            args.pop(0)
        elif args and (args[0] == '--no-commit'):
            commit = False
            args.pop(0)

    if len(args) != 1 or not re_ga.match(args[0]):
        sys.stderr.write(f"Usage: {os.path.basename(sys.argv[0])} X.Y.Z\n")
        sys.exit(2)

    sys.exit(main(
        next_ver=args[0],
        today=datetime.date.today(),
        quiet=quiet,
        commit=commit,
    ))
